![](/img/trans.png)
[英]PopUp not sending data in database in ASP.NET MVC 5 and Entity Framework
[英]Recipe - ingredients database in Entity Framework ASP.NET MVC
这将创建两个表“ Ingredient”和“ Recipe”,以及一个用于多对多映射的附加表。
public class DC : DbContext {
public DbSet<Ingredient> Ingredients { get; set; }
public DbSet<Recipe> Recipes { get; set; }
}
public class Ingredient {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Recipe> Recipes { get; set; }
}
public class Recipe {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Ingredient> Ingredients { get; set; }
}
问题:我想在将由Entity Framework创建的第三个映射表中包含附加列“quantity”。 如何做到这一点? 提前致谢。
当您获得一些额外的信息时,我怀疑它真的不会再算作映射表了-它不仅仅是多对多映射。 我认为您应该将其建模为另一个表:
public class Ingredient {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RecipePart> RecipeParts { get; set; }
}
public class RecipePart {
public int Id { get; set; }
public Ingredient { get; set; }
public Recipe { get; set; }
// You'll want to think what unit this is meant to be in... another field?
public decimal Quantity { get; set; }
}
public class Recipe {
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<RecipePart> Parts { get; set; }
}
因此,现在您实际上并没有多对多映射-您有两个普通的多对一映射。 你肯定需要在你的模型中暴露出“配料到配方”的映射吗? 如果您想查找使用特定成分的所有食谱,您可以随时进行查询,例如:
var recipies = DB.Recipies.Where(r => r.Parts
.Any(p => p.Ingredient == ingredient));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.