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();
}
}
}