我使用VS项目中的代码优先迁移生成了一个localDB . 我在两个实体( Recipe
和 Ingredient
)之间存在多对多关系 .
我的模型类:
public class Recipe
{
[Key]
public int RecipeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<Ingredient> Ingredients { get; set; }
}
public class Ingredient
{
[Key]
public int IngredientId { get; set; }
[Required]
public string Name { get; set; }
public bool Organic { get; set; }
public bool Vegan { get; set; }
public virtual ICollection<Recipe> Recipes { get; set; }
}
我用EF生成的模式结果如下:
一个名为 RecipeIngredients
的连接表包含两个外键( RecipeId
和 IngredientId
),这就是我所追求的,但......
Problem: 我想在 RecipeIngredient
表中有一个额外的列( Quantity
) . 我该如何解决这个问题?
1 回答
无法向Entity Framework自动生成的连接表添加任何其他列 .
您应该创建一个包含所需信息的新实体
RecipeIngredient
,并在RecipeIngredient
和Ingredient
之间定义一对多关系,在RecipeIngredient
和Ingredient
之间定义另一个关系 .