我有几个模型和一个数据库,但没有将任何东西链接在一起的外键。这似乎是我项目中的一个巨大弱点,所以我试图将外键插入我的模型中,然后可能会根据我的模型重新生成我的数据库。
我在理解外键如何工作时遇到了一些麻烦,尤其是在一对多的关系中。
对于这个例子,我有一个或多个产品,每个产品可能有多个评论:
我删除了一些注释和属性来压缩模型。
产品型号/实体:
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 到 Review 的 ProductId 的外键约束。我将如何进行这项工作?