1

How do I separate my concerns please so that I could have sub categories on each product categories. Example Car category will point to new cars and old cars. Below are my category class and product class code that generate my database. Thanks for your help.

Category class:

public class Category
{
    [ScaffoldColumn(false)]
    public int CategoryID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string CategoryName { get; set; }

    [Display(Name = "Product Description")]
    public string Description { get; set; }

    public virtual ICollection<Product> Products { get; set; }

 }

Product Class:

 public class Product
{

    [ScaffoldColumn(false)]
    public int ProductID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string ProductName { get; set; }

    public string Description { get; set; }

    public string ImagePath { get; set; }

    [Display(Name = "Price")]
    public double? UnitPrice { get; set; }

    [Required, StringLength(100), Display(Name = "Seller")]
    public string Seller { get; set; }

    public DateTime Date { get; set; }

    public int? CategoryID { get; set; }

    public virtual Category Category { get; set; }

}

Context class:

public class ProductContext : DbContext 
{
    public ProductContext() : base("my")
    {

    }
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<CartItem> ShoppingCartItems { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    public DbSet<State> States { get; set; }

}

Meanwhile I tried to follow this related topic in stockoverflow but It's a bit confusing. Thanks for taking your time to help me.

dbraillon
  • 1,742
  • 2
  • 22
  • 34
Simple_tech
  • 39
  • 10

1 Answers1

2

This is what we need to achieve:

Db scheme

One category has many subcategories, and one subcategory has many products.

  • Product owns foreign key that references to Subcategory.
  • Subcategory owns the list of products that have the certain subcategory.
  • Subcategory owns the foreign key that references to Category.
  • Category owns the list of subcategories that have the certain category.

So, your models will look like this:


Product.cs

public class Product
{

    [ScaffoldColumn(false)]
    public int ProductID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string ProductName { get; set; }

    public string Description { get; set; }

    public string ImagePath { get; set; }

    [Display(Name = "Price")]
    public double? UnitPrice { get; set; }

    [Required, StringLength(100), Display(Name = "Seller")]
    public string Seller { get; set; }

    public DateTime Date { get; set; }

    public int? SubcategoryID { get; set; }
    public virtual Subcategory Subcategory { get; set; }
}

Category.cs

public class Category
{
    [ScaffoldColumn(false)]
    public int CategoryID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string CategoryName { get; set; }

    [Display(Name = "Category Description")]
    public string Description { get; set; }

    public virtual ICollection<Subcategory> Subcategories { get; set; }
}

Subcategory.cs

public class Subcategory
{
    [ScaffoldColumn(false)]
    public int SubcategoryID { get; set; }

    [Required, StringLength(100), Display(Name = "Name")]
    public string SubcategoryName { get; set; }

    [Display(Name = "Subcategory Description")]
    public string Description { get; set; }

    public int? CategoryID { get; set; }
    public virtual Category Category { get; set; }

    public virtual ICollection<Product> Products { get; set; }
}

ProductContext .cs

public class ProductContext : DbContext 
{
    public ProductContext() : base("my")
    {

    }

    public DbSet<Category> Categories { get; set; }
    public DbSet<Subcategory> Subcategories { get; set; }
    public DbSet<Product> Products { get; set; }
    public DbSet<CartItem> ShoppingCartItems { get; set; }
    public DbSet<Order> Orders { get; set; }
    public DbSet<OrderDetail> OrderDetails { get; set; }
    public DbSet<State> States { get; set; }
}
Anis Alibegić
  • 2,941
  • 3
  • 13
  • 28
  • Thank you very much for your help. This work fine for me, but I also retain my CategoryID property within the Product class in addition to the one you supply. I did this to make the product table look more clearer. – Simple_tech Apr 04 '18 at 13:19
  • How can I assign multiple subCategories to a product in the same example? I'm kind of new to EF and I'm trying to wrap my head around for this since 2 days. I'll really appreciate your help. – ChiragMS Nov 29 '19 at 07:06