Entity Framework6.1.3;代码优先迁移多对多加入 table
Entity Framework 6.1.3; code first migrations many to many joined table
我在我的 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
的连接 table 包含两个外键(RecipeId
和 IngredientId
),这正是我所追求的,但是...
问题: 我想在 RecipeIngredient
table 中有一个额外的列 (Quantity
)。我该如何解决?
无法向 Entity Framework 自动生成的连接 table 添加任何附加列。
您应该使用您需要的所有信息创建一个新实体 RecipeIngredient
,并定义 RecipeIngredient
和 Ingredient
之间的一对多关系以及 RecipeIngredient
和 RecipeIngredient
之间的另一个关系Ingredient
.
public class Recipe {
[Key]
public int RecipeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<RecipeIngredient> 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<RecipeIngredient> Recipes { get; set; }
}
public class RecipeIngredient {
[Key, Column(Order = 0)]
public int RecipeID { get; set; }
[Key, Column(Order = 1)]
public int IngredientID { get; set; }
public double Quantity { get; set; }
}
我在我的 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
的连接 table 包含两个外键(RecipeId
和 IngredientId
),这正是我所追求的,但是...
问题: 我想在 RecipeIngredient
table 中有一个额外的列 (Quantity
)。我该如何解决?
无法向 Entity Framework 自动生成的连接 table 添加任何附加列。
您应该使用您需要的所有信息创建一个新实体 RecipeIngredient
,并定义 RecipeIngredient
和 Ingredient
之间的一对多关系以及 RecipeIngredient
和 RecipeIngredient
之间的另一个关系Ingredient
.
public class Recipe {
[Key]
public int RecipeId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public virtual ICollection<RecipeIngredient> 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<RecipeIngredient> Recipes { get; set; }
}
public class RecipeIngredient {
[Key, Column(Order = 0)]
public int RecipeID { get; set; }
[Key, Column(Order = 1)]
public int IngredientID { get; set; }
public double Quantity { get; set; }
}