【问题标题】:EntityTypeConfiguration validation errorsEntityTypeConfiguration 验证错误
【发布时间】:2014-10-31 22:51:02
【问题描述】:

我在映射这些实体时遇到问题。我对此有点陌生,所以我不确定我在哪里搞砸了。任何建议或帮助将不胜感激。

这是错误: 在模型生成期间检测到一个或多个验证错误:

Recipe_RecipeIngredients_Source: : 多重性在关系“Recipe_RecipeIngredients”中的角色“Recipe_RecipeIngredients_Source”中无效。因为从属角色是指关键属性,所以从属角色的多重性的上限必须是'1'。

这是我的两张表的示意图

我的映射代码如下:

public class RecipeMap : EntityTypeConfiguration<Recipe>
{
    public RecipeMap()
    {
        HasKey(g => g.RecipeId);
        ToTable("recipes");
        Property(g => g.RecipeId).HasColumnName("RecipeId");
        Property(g => g.Title).HasColumnName("Title");
        Property(g => g.Description).HasColumnName("Description");
        Property(g => g.IsOnMenu).HasColumnName("IsOnMenu");
        Property(g => g.Url).HasColumnName("Url");
        Property(g => g.ImagePath).HasColumnName("ImagePath");
        Property(g => g.Calories).HasColumnName("Calories");
        Property(g => g.Servings).HasColumnName("Servings");
        Property(g => g.TotalTime).HasColumnName("TotalTime");
        Property(g => g.PrepTime).HasColumnName("PrepTime");
        Property(g => g.CookTime).HasColumnName("CookTime");
        this.HasRequired(g => g.RecipeIngredients).WithMany().HasForeignKey(x => x.RecipeId);

    }
}

public class RecipeIngredientMap : EntityTypeConfiguration<RecipeIngredient>
{
    public RecipeIngredientMap()
    {
        HasKey(g => g.IngredientId);
        ToTable("recipeIngredient");
        Property(g => g.RecipeId).HasColumnName("RecipeId").IsRequired();
        Property(g => g.IngredientName).HasColumnName("IngredientName");
        Property(g => g.Quantity).HasColumnName("Quantity");
        Property(g => g.Category).HasColumnName("Category");
        Property(g => g.IsOnMenu).HasColumnName("IsOnMenu");
        //this.HasRequired(g => g.Recipe);
    }
}

 public class RecipeContext : DbContext
{
    public virtual DbSet<RecipeIngredient> RecipeIngredients { get; set; }
    public virtual DbSet<Recipe> Recipes { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Configurations.Add(new RecipeMap());
        modelBuilder.Configurations.Add(new RecipeIngredientMap());
    }
}

【问题讨论】:

  • 出了什么问题,是否存在某种错误,或者数据没有按照您的预期填充?
  • 即使我只是为 recipeIngredients 取出 HasRequired 我也没有收到错误,但是当我调用 context.RecipeIngredients.ToList() 时什么也没有

标签: c# sql asp.net sql-server entity-framework


【解决方案1】:

我已经有一段时间没有玩实体框架了,所以这可能并不完全正确。

我假设你的课程看起来像这样:

public class Recipe
{
    public int ID { get; set; }
    public string Title { get; set; }

    // Other code

    // Navigation property
    public virtual ICollection<RecipeIngredient> Ingredients { get; set; }
}

public class RecipeIngredient
{
    public int ID { get; set; }
    public int RecipeID { get; set; }
    // Other code
}

我认为你试图描述的关系(我可能不正确)是Recipe 有很多Ingredients,所以我会在你的RecipeMap 中尝试这个(使用你自己的属性名称):

this.HasMany(x => x.Ingredients) // A Recipe has many ingredients.
    .WithRequired() // Ingredient requires a recipe.
    .HasForeignKey(x => x.RecipeID); // Our Foreign Key on Ingredient.

顺便说一句,您的数据模型似乎可以通过引入一个连接/链接表来进一步规范化,例如 RecipeIngredients(或转换您已有的),其中包含字段 RecipeIDIngredientIDQuantity.

然后您可以将与Ingredient 专门相关的所有字段提取到Ingredients 表中,并保存一些重复的成分数据。

【讨论】:

  • 感谢您的帮助。对此,我真的非常感激。一旦我记得我改变了我的连接字符串的名称,这很完美。我真的在挑战自己去学习这方面的东西,我觉得我实际上学到了很多东西。但有时我会迷失它的好处,以至于 Stackoverflow 的存在。
  • 很高兴它有帮助,我对这个话题有点生疏了,但是当我使用流利的语法手动配置映射时,我发现尝试描述上面的代码/阅读中的关系很有帮助大声说出来。这其中有许多,需要其中之一等。也值得玩重载,例如WithRequired 可以接受Func,如果需要,它允许RecipeIngredient 上的Recipe 导航属性。祝你好运=D
【解决方案2】:

一个配方有多种成分,因为您似乎在映射中暗示了错误的关系。

从RecipeMap() 中删除hasRequired 并将以下内容添加到RecipeIngredientMap() 中

 this.HasRequired(g => g.Recipes)
.WithMany(y => y.RecipesIngredient)
.HasForeignKey(x => x.RecipeId);

【讨论】:

  • 那不会编译。像这样的东西? this.HasRequired(g => g.Recipe).WithMany(x => x.RecipeIngredients).HasForeignKey(y => y.RecipeId);但是,当我执行 Context.Recipes.ToList() 时,我仍然没有从上下文中得到任何信息,所以我在某处遗漏了一些东西。
  • 确保您的课程看起来像@Chris 建议的那样,并且您所做的更改是正确的,我的错..
猜你喜欢
  • 2011-05-22
  • 2013-02-17
  • 1970-01-01
  • 2018-09-05
  • 2011-07-08
  • 2014-04-11
  • 2013-02-18
  • 2017-04-09
  • 2016-01-21
相关资源
最近更新 更多