Click here to Skip to main content
14,980,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing this error create db through data module then create a class for call db tables and then create a class to access services and create project but facing error.

What I have tried:

When access database through database first approach automativally created below class

C#
namespace Ecommerce.DBs.Database
{
    using System;
    using System.Collections.Generic;
    
    public partial class Tbl_Category
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Tbl_Category()
        {
            this.Tbl_Product = new HashSet<tbl_product>();
        }
    
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public Nullable<bool> IsActive { get; set; }
        public Nullable<bool> IsDelete { get; set; }
        public bool IsFeatured { get; set; }
        public string ImageURL { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<tbl_product> Tbl_Product { get; set; }

        public List<tbl_product> Products { get; set; }
    }
}


after that i have created 1 more class to access above

C#
namespace Ecommerce.DBs
{
    public class EcomContext : DbContext, IDisposable
    {

        public EcomContext() : base("OnlineShoppingEntities")
        {

        }
        public DbSet<tbl_category> Categories { get; set; }
        public DbSet<tbl_product> Products { get; set; }
    }
}


created a new project and add class with same table name as below

C#
namespace Ecommerce.Entities
{
   public  class Tbl_Category
    {
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public bool IsActive { get; set; }
        public bool IsDelete { get; set; }
        public bool IsFeatured { get; set; }
        public string ImageURL { get; set; }

        public List<product> Products { get; set; }
    }
}

after that created 1 more project with name of services and add class as below

C#
namespace Ecommerce.Services
{
   public  class CategoriesService
    {
        public Tbl_Category GetCategoryID(int id)
        {
            using (var context = new EcomContext())
            {
                return context.Categories.Find(id);
            }
        }

        public List<tbl_category> GetFeaturedCategory()
        {
            using (var context = new EcomContext())
            {
                return context.Categories.Where(x => x.IsFeatured && x.ImageURL != null).ToList();
            }
        }
}}


now finally created a home controller in web project when going to call above class in home controller below error is showing i tried diffrent logics but errro still is same if i remove database first approach then error will be cleared but i need database first approach.

C#
public class HomeController : Controller
{
    <big>CategoriesService categoryService = new CategoriesService();</big>
    public ActionResult Index()
    {
        HomeViewModels model = new HomeViewModels();
        model.Featuredcategories = categoryService.GetFeaturedCategory();
        return View(model);
    }
}
}


Error Shows
cannot implicity convert type system.collections.generic.list<ecommerce.entities.tbl_category> to system.collections.generic.list<ecommerce.entities.category>
Posted
Updated 16-May-21 10:49am
v3
Comments
BillWoodruff 16-May-21 0:31am
   
highlight in your code exactly where the error occurs
Shoaib Shafiq 16-May-21 5:56am
   
in below code the error occurs

public class HomeController : Controller
{
CategoriesService categoryService = new CategoriesService();
public ActionResult Index()
{
HomeViewModels model = new HomeViewModels();
model.Featuredcategories = categoryService.GetFeaturedCategory();
return View(model);
}
}
}
Richard MacCutchan 16-May-21 6:50am
   
I have corrected your formatting so the code is readable. In future please use the correct <pre> tags to format code samples.
Shoaib Shafiq 16-May-21 6:59am
   
thanks...
Richard MacCutchan 16-May-21 6:51am
   
The error message clearly tells you tat you cannot use different types to initialise variables.
Shoaib Shafiq 16-May-21 7:00am
   
Richard MacCutchan
need suggestion please advise.
Richard MacCutchan 17-May-21 3:54am
   
As I said, the error message is clear. So look at your code, and you can see that you are trying to use a list of ecommerce.entities.tbl_category types, where you are expecting a list of ecommerce.entities.category types. And it is reasonable to assume that a tbl_category and a category, are quite different.

1 solution

Your category entity class is defined as Tbl_Category (note the starting CAP); yet in your code, you reference it generically as "tbl_Category" (note the lower case).

You probably have the same issue with tbl_product (versus Tbl_product)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900