Pri*_*cey 2 c# ef-code-first asp.net-mvc-3 entity-framework-4.3
我试图找出导致此错误的原因,我列出了我的代码中的一些相关区域,希望有助于解决我的问题.
配方实体的成员集合如下所示:
public virtual IList<Member> Members { get; set; }
Run Code Online (Sandbox Code Playgroud)
这是成员实体上的Recipes集合:
public virtual IList<Recipe> Recipes { get; set; }
Run Code Online (Sandbox Code Playgroud)
我在创建DbContext时执行以下操作,以便在单独的表中建立多对多关系
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// have to specify these mappings using the EF Fluent API otherwise I end up with
// the foreign key fields being placed inside the Recipe and Member tables, which wouldn't
// give a many-to-many relationship
modelBuilder.Entity<Recipe>()
.HasMany(r => r.Members)
.WithMany(m => m.Recipes)
.Map(x => {
x.ToTable("Cookbooks"); // using a mapping table for a many-to-many relationship
x.MapLeftKey("RecipeId");
x.MapRightKey("MemberId");
});
modelBuilder.Entity<Recipe>()
.HasRequired(x => x.Author)
.WithMany()
.WillCascadeOnDelete(false);
}
Run Code Online (Sandbox Code Playgroud)
当模型发生变化时我也会为我的数据库播种,而我所要做的就是添加到食谱的成员集合中,它似乎能够为我排序剩下的部分并将相关的密钥放在我的食谱关系表中.
这是我的配方控制器操作中执行工作的一些代码:
var memberEntity = memberRepository.Find((int)memberId);
var recipeEntity = recipeRepository.Find(recipeId);
recipeEntity.Members.Add(memberEntity);
recipeRepository.InsertOrUpdate(recipeEntity);
recipeRepository.Save();
Run Code Online (Sandbox Code Playgroud)
这是我的Recipe存储库上的插入或更新方法
public void InsertOrUpdate(Recipe recipe)
{
if (recipe.Id == default(int))
{
// New entity
context.Recipes.Add(recipe);
} else
{
// Existing entity
context.Entry(recipe).State = EntityState.Modified;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到错误"InvalidOperationException:无法定义两个对象之间的关系,因为它们附加到不同的ObjectContext对象." 在这条线上:
context.Entry(recipe).State = EntityState.Modified;
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么会这样?我是否必须将该成员添加到配方中,反之亦然才能使其工作?我不确定问题是什么,因为recipeEntity似乎是正确的.
任何帮助将不胜感激,谢谢.
编辑正在每个存储库(RecipeRepository和MemberRepository)中创建上下文,如图所示,所以我认为这是一个问题,因为每个.Find()请求都使用不同的上下文?这会导致问题吗?
private EatRateShareDbContext context = new EatRateShareDbContext();
Run Code Online (Sandbox Code Playgroud)
我不确定这是解决方案,但似乎您在存储库中使用了不同的上下文.
首先确保每个生命周期都有相同的上下文.根据您的项目类型,生命周期可能会有所不同 (例如,对于Web项目,通常每个HttpContext都是相同的).您可以使用IoC来管理上下文生命周期.适用于.Net的优秀IoC库是autofac和Castle Windsor
另外,我认为您对InsertOrUpdate方法的调用是不必要的(除非您调用Find没有跟踪的方法.)只需删除该行并查看它是否有效:
var recipeEntity = recipeRepository.Find(recipeId);
recipeEntity.Members.Add(memberEntity);
recipeRepository.Save();
Run Code Online (Sandbox Code Playgroud)
这里HttpRequest提到了一种分享DbContext的简单方法.
| 归档时间: |
|
| 查看次数: |
10962 次 |
| 最近记录: |