C# 实体框架ASP.NET MVC中的配方-配料数据库

C# 实体框架ASP.NET MVC中的配方-配料数据库,c#,asp.net-mvc-3,entity-framework-4,C#,Asp.net Mvc 3,Entity Framework 4,这将创建两个表“配料”和“配方”,并为多对多映射创建一个附加表 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 st

这将创建两个表“配料”和“配方”,并为多对多映射创建一个附加表

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; }
}
公共类DC:DbContext{
公共数据库集{get;set;}
公共数据库集配方{get;set;}
}
公共类成分{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection配方{get;set;}
}
公共课食谱{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共虚拟ICollection组件{get;set;}
}

问题:我想在EntityFramework将创建的第三个映射表中包含额外的列“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; }
}

谢谢您的帮助,但由于我是实体框架的新手,我有一个关于您提供的RecipePart类的问题。应该是“公共成分{get;set;};”还是“公共成分{get;set;}”?@Acute:恐怕我不能说——我自己对EF了解不够。
var recipies = DB.Recipies.Where(r => r.Parts
                                       .Any(p => p.Ingredient == ingredient));