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
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
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
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
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.
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>