简体   繁体   English

如何首先使用实体​​框架代码在Asp.net中添加子类别

[英]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: 这是我们需要实现的目标:

db方案

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 产品.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 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 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 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; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用“代码优先实体框架”在ASP.NET MVC 4中创建数据库并将其连接到数据库? - How to create a database and connect to it in ASP.NET MVC 4 by using Code First Entity Framework? 实体框架(代码优先)-&ASP.net成员资格提供程序 - Entity framework(Code first) - & ASP.net Membership Provider 代码优先实体框架Asp.net MVC - Code First Entity Framework Asp.net MVC 实体框架6 asp.net代码优先设置 - Entity Framework 6 asp.net Code First Setup 实体框架-ASP.NET表单网站中没有代码优先选项 - Entity framework - No code first option in asp.net form website 具有实体框架代码优先导航属性的ASP.Net MVC - ASP.Net MVC with Entity Framework Code First Navigation Property 实体框架代码首先是ASP.Net和DbContext - Entity Framework Code First ASP.Net and DbContext 使用实体框架的代码优先方法在ASP.NET MVC应用程序中缺少外键关系 - Foreign key relationship missing in ASP.NET MVC app using code-first approach with Entity Framework 在ASP.NET Web表单中使用Entity Framework(代码优先方法)在Repeater控件中显示详细信息 - Show details in Repeater control using Entity Framework (code first approach) in asp.net webforms 在ASP.NET MVC项目中使用实体框架代码优先时登录失败错误 - Login failed error when using Entity Framework code-first in ASP.NET MVC project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM