title: EFCodeFirst : The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects
body: <p>I am trying to find out what is causing this error, I have listed some of the relevant areas of my code that should hopefully help answer my problem.</p>
  
  <p>The recipe entity's members collection is as shown below:</p>
  
  <pre><code>public virtual IList&lt;Member&gt; Members { get; set; }
  </code></pre>
  
  <p>and here is the Recipes collection on the member entity:</p>
  
  <pre><code>public virtual IList&lt;Recipe&gt; Recipes { get; set; }
  </code></pre>
  
  <p>I do the below when creating my DbContext in order to make a many-to-many relationship in a separate table</p>
  
  <pre><code>    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&lt;Recipe&gt;()
              .HasMany(r =&gt; r.Members)
              .WithMany(m =&gt; m.Recipes)
          .Map(x =&gt; {
              x.ToTable("Cookbooks"); // using a mapping table for a many-to-many relationship
              x.MapLeftKey("RecipeId");
              x.MapRightKey("MemberId");
          });
  
          modelBuilder.Entity&lt;Recipe&gt;()
              .HasRequired(x =&gt; x.Author)
              .WithMany()
              .WillCascadeOnDelete(false);
  
      }
  </code></pre>
  
  <p>I also seed my database when the model changes and all I have had to do is add to a recipe's member collection and it seems to be able to sort the rest out for me and place the relevant keys in my cookbook relationship table.</p>
  
  <p>This is some of the code in my recipe controller action that performs the work:</p>
  
  <pre><code>var memberEntity = memberRepository.Find((int)memberId);
  var recipeEntity = recipeRepository.Find(recipeId);
  recipeEntity.Members.Add(memberEntity);
  recipeRepository.InsertOrUpdate(recipeEntity);
  recipeRepository.Save();
  </code></pre>
  
  <p>Here is the insert or update method on my Recipe repository</p>
  
  <pre><code>    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;
          }
      }
  </code></pre>
  
  <p>I get an error of "InvalidOperationException : The relationship between the two objects cannot be defined because they are attached to different ObjectContext objects." on this line:</p>
  
  <pre><code>context.Entry(recipe).State = EntityState.Modified;
  </code></pre>
  
  <p>Does anyone know why that would happen? Do I have to add the member to the recipe and vice versa to get this to work? I'm not sure what the problem is because the recipeEntity seems to be the correct one.</p>
  
  <p>Any help would be appreciated, thanks.</p>
  
  <p><strong>EDIT
  The context is being created in each repository (RecipeRepository &amp; MemberRepository) as shown, so I presume this is the problem in that a different context is being used for each .Find() request? and that causes problems?</strong></p>
  
  <pre><code>private EatRateShareDbContext context = new EatRateShareDbContext();
  </code></pre>