Asp.net 在代码优先实体框架中设计类
我正在构建一个在线购物web应用程序。 我有四个实体:Asp.net 在代码优先实体框架中设计类,asp.net,entity-framework-4,ef-code-first,data-annotations,code-first,Asp.net,Entity Framework 4,Ef Code First,Data Annotations,Code First,我正在构建一个在线购物web应用程序。 我有四个实体: 顾客 库索体 名字 地址 电话 类别 类别 类别 产品 产品ID 名字 价格 类别 命令 医嘱ID 客户ID 产品ID 数量 数量 请帮助我设计类,以便保持适当的关系(主键和外键)。 我尝试过这样做,但数据库本身正在创建外键,这导致了不一致性 我的班级: public class Category { public int CategoryId { get; set; } public str
- 库索体
- 名字
- 地址
- 电话
- 类别
- 类别
- 产品ID
- 名字
- 价格
- 类别
- 医嘱ID
- 客户ID
- 产品ID
- 数量
- 数量
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class Order
{
public int CustomerID { get; set; }
public int OrderID { get; set; }
public DateTime PurchaseDate { get; set; }
public string ProductName { get; set; }
public List<Product> Products { get; set; }
public List<Customer> Customers { get; set; }
}
public class Product
{
public int ProductId{ get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public string Category { get; set; }
public List<Category> Categories { get; set; }
}
公共类类别
{
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
}
公共类客户
{
public int CustomerID{get;set;}
公共字符串名称{get;set;}
公共字符串地址{get;set;}
公共字符串电子邮件{get;set;}
公共字符串密码{get;set;}
}
公共阶级秩序
{
public int CustomerID{get;set;}
公共int-OrderID{get;set;}
public DateTime PurchaseDate{get;set;}
公共字符串ProductName{get;set;}
公共列表产品{get;set;}
公共列表客户{get;set;}
}
公共类产品
{
public int ProductId{get;set;}
公共字符串ProductName{get;set;}
公共整数价格{get;set;}
公共字符串类别{get;set;}
公共列表类别{get;set;}
}
问题在于决定什么应该成为主键和外键,以及如何成为主键和外键?
当前数据库正在将
订单\u orderid
字段作为外键添加到产品
表中。您的问题是您正在使用订单中的列出产品
。据我所知,这片土地不应该在这里。您应该创建另一个表areOrderItem
,该表将包含列表产品
。您可以下载数据库以获得更多了解
回到你的问题上来。您应该将列出产品
属性从类中删除订单
,并重新生成数据库。存在许多问题:
- 导航属性上缺少虚拟链接
- 考虑引入OrderItem表,否则在Order表中每个项目都需要一个代理键
- 如果您想自己创建ForeignKey ID字段,则需要注释或Fluent API来告诉EF有关该字段的信息
这里解释得很好
我自己解决了,现在效果很好。以下是课程:
public class Category
{
public int CategoryId { get; set; }
public string CategoryName { get; set; }
}
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Email { get; set; }
public string Password { get; set; }
}
public class Product
{
public int ProductId{ get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public int CategoryId { get; set; }
public virtual Category Categories { get; set; }
}
public class Order
{
public int CustomerID { get; set; }
public int OrderID { get; set; }
public int ProductId { get; set; }
public DateTime PurchaseDate { get; set; }
public string ProductName { get; set; }
public int Price { get; set; }
public string DeliveryStatus { get; set; }
public virtual Product Products { get; set; }
public virtual Customer Customers { get; set; }
}
这为我提供了理想的数据库设计。您应该显示从EF生成的图像数据库。这将有助于您得到正确的答案。@ToanVo:我已经用一些代码更新了我的问题,请看您是否能提供帮助?