0

Product and Category table have a one to many relation. I want to get category name from product but getting some error. my code is working on creating new dabase first project but not working on asp.net core project.

 public class Category : IEntity
    {
        public Category()
        {
            Children = new List<Category>();
            Products = new List<Product>();
        }
        public int CategoryId { get; set; }
        public string CategoryName { get; set; }
        public int? ParentCategoryId { get; set; }
        public string Description { get; set; }
        public bool IsActive { get; set; }
        public bool? IsParent { get; set; }
        public string LangCode { get; set; }
        public virtual Category Parent { get; set; }
        public virtual ICollection<Category> Children { get; set; }
        public virtual ICollection<Product> Products { get; set; }
    }



public class Product : IEntity
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public int? CategoryId { get; set; }
        public decimal UnitPrice { get; set; }
        public short? UnitsInStock { get; set; }
        public string Description { get; set; }
        public string ProductCode { get; set; }
        public bool? IsActive { get; set; }
        public string UnitCode { get; set; }
        public double? GrossWeight { get; set; }
        public double? NetWeight { get; set; }
        public DateTime? CreateDate { get; set; }
        public DateTime? UpdateDate { get; set; }
        public int? CreateUser { get; set; }
        public int? UpdateUser { get; set; }
        public double? Length { get; set; }
        public double? Width { get; set; }
        public double? Height { get; set; }
        public string SizeUnitCode { get; set; }
        public string Brand { get; set; }
        public string AlternateProductCode { get; set; }
        [ForeignKey("CategoryId")]
        public virtual Category Category { get; set; }
    }

 public class DataContext:DbContext
{
    public static string ConnectionString { get; set; }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {         
        optionsBuilder.UseSqlServer(ConnectionString);
    }
    public DbSet<Page> Pages { get; set; }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Category>()
                .HasOne(i => i.Parent)
                .WithMany(i => i.Children)
                .HasForeignKey(i => i.ParentCategoryId);
        modelBuilder.Entity<Product>()
            .HasOne(p => p.Category)
            .WithMany(b => b.Products)
            .HasForeignKey(f => f.CategoryId);
    }
}
public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<IPageService, PageManager>();
            services.AddScoped<IPageDal, EfPageDal>();
            services.AddScoped<ICategoryService, CategoryManager>();
            services.AddScoped<ICategoryDal, EfCategoryDal>();
            services.AddScoped<IProductService, ProductManager>();
            services.AddScoped<IProductDal, EfProductDal>();
            services.AddSession();
            services.AddDistributedMemoryCache();
            services.AddMvc();
            DataContext.ConnectionString = Configuration.GetConnectionString("DefaultConnection");
        }

   public class EfEntityRepositoryBase<TEntity, TContext> : IEntityRepository<TEntity>
        where TEntity : class, IEntity, new()
        where TContext : DbContext, new()
    {


        public List<TEntity> GetList(Expression<Func<TEntity, bool>> filter = null)
        {
            using (var context = new TContext())
            {
                return filter == null
                    ? context.Set<TEntity>().ToList()
                    : context.Set<TEntity>().Where(filter).ToList();
            }
        } 
    }

screen

jpp28
  • 61
  • 1
  • 18
FDemirci
  • 1
  • 3
  • 2
    you probably need `.Include(x => x.Category)` in your repository method. btw setting everything to inherit from a base entity -- be weary of entity framework putting everything into a megatable with a discriminator (Table Per Hierarchy) – Mardoxx Jul 12 '17 at 15:06
  • Tanks for answer @Mardoxx. I don't understand where using you sugested Include code. i'm added in question my repository method. Can you check my repository method. – FDemirci Jul 12 '17 at 19:55

1 Answers1

0

I Get data like following code. But this code is stupid. I'm waiting for good idea solving that problem.

public ActionResult Index()
{
    ProductListViewModel model = new ProductListViewModel
    {
        Products = _productService.GetAll()
    };

    model.Products.ForEach(b =>
    {
        b.ProductCategory = _categoryService.GetByID(b.CategoryId);
    });

    return View(model);
}
FDemirci
  • 1
  • 3