【发布时间】:2012-05-07 17:30:49
【问题描述】:
我有几个模型和一个数据库,但没有将任何东西链接在一起的外键。这似乎是我项目中的一个巨大弱点,所以我试图将外键插入我的模型中,然后可能会根据我的模型重新生成我的数据库。
我在理解外键如何工作时遇到了一些麻烦,尤其是在一对多关系中。
对于这个例子,我有一个或多个产品,每个产品可能有多个评论:
我删除了一些注释和属性来压缩模型。
产品型号/实体:
public class Product
{
public int ProductId { get; set; }
public int OwnerId {get; set; }
public string Title { get; set; }
public int Rating { get; set; }
public decimal Price { get; set; }
public int Quantity { get; set; }
public string Description { get; set; }
}
审查模型/实体:
public class Review
{
public int ReviewId { get; set; }
public int ProductId { get; set; }
public int WriterId { get; set; }
public string Title { get; set; }
public string Body { get; set; }
}
我希望 ProductId 的外键约束到审查的 ProductId。我将如何完成这项工作?
【问题讨论】:
标签: asp.net-mvc-3 entity-framework