【发布时间】: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