[英]How to add Sub Categories in Asp.net using Entity framework code first
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.
感谢您抽出宝贵的时间来帮助我。
This is what we need to achieve: 这是我们需要实现的目标:
One category has many subcategories , and one subcategory has many products . 一类具有许多子类别 , 一类具有许多产品 。
So, your models will look like this: 因此,您的模型将如下所示:
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; }
}
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; }
}
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; }
}
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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.