title: Insert one to many record
body: <p>I'm facing a problem regarding inserts with the Entity Framework.</p>
  
  <p>My application has the following two entities:</p>
  
  <ul>
  <li>Memo</li>
  <li>Employee</li>
  </ul>
  
  <p>A Memo can be linked with multiple Employees.<br/>
  A Employee can be linked with multiple Memos.<br/></p>
  
  <p>Which means a many to many relation.
  I read a few articles explaining me that a junction table should be created, which I think is obvious.</p>
  
  <p>The articles learned me to let Entity Framework automatically create a junction table for me. So I did this the following way:</p>
  
  <p><strong>Memo</strong></p>
  
  <pre><code>public Guid MemoId { get; set; }
  public String Message { get; set; }
  public virtual ICollection&lt;Employee&gt; Employees { get; set; }
  </code></pre>
  
  <p><strong>Employee</strong></p>
  
  <pre><code>public Guid EmployeeId { get; set; }
  public String Name { get; set; }
  public virtual ICollection&lt;Memo&gt; Memos { get; set; }
  </code></pre>
  
  <p>When updating my database using the Package Manager Console, a junction table has been created in the database. I did this using following line:<br/>
  <code>update-database -force -verbose</code></p>
  
  <p>I have a view for creating a new Memo.
  It is possible to select a list of Employees here and add them to the Memo.
  However filling this junction table isn't going as planned. I think it has something to do with the setup of my repositories. I created a MemoRepository and a EmployeeRepository.</p>
  
  <p>My controller handling the Memo create is as follow:<br/><br/>
  <strong>MemoController</strong></p>
  
  <pre><code>public class MemoController : Controller
  {
      private IMemoRepository _memoRepository;
      private IEmployeeRepository _employeeRepository;
  
      public MemoController(IMemoRepository memoRepository, IEmployeeRepository employeeRepository) {
          _memoRepository = memoRepository;
          _employeeRepository = employeeRepository;
      }
  
      public ViewResult Create() {
          //Initializes MemoCreateViewModel here
          return View(model);
      }
  
      [HttpPost]
      public ActionResult Create(MemoCreateViewModel model) {
          if(!ModelState.IsValid)
              return RedirectToAction("Create");
  
          Guid employeeId;
          List&lt;Guid&gt; employeeIds = new List&lt;Guid&gt;();
          foreach (var id in model.SelectedEmployeeIds) {
              if (!Guid.TryParse(id, out employeeId)) {
                  continue;
              }
              employeeIds.Add(employeeId);
          }
          var employees = _employeeRepository.GetEmployeesByIds(employeeIds);
          model.Memo.Employees = employees.ToList&lt;Employee&gt;();
          _memoRepository.SaveMemo(model.Memo);
  
          return RedirectToAction("List");
      }
  }
  </code></pre>
  
  <p><strong>MemoRepository</strong></p>
  
  <pre><code>public class EFMemoRepository : IMemoRepository
  {
      private EFDbContext context;
  
      public EFMemoRepository(EFDbContext _context) {
          context = _context;
      }
  
      public IQueriable&lt;Memo&gt; Memos {
          return context.Memos;
      }
  
      public void SaveMemo(Memo memo) {
          if(memo.MemoId == Guid.Empty) {
              memo.MemoId = Guid.NewGuid();
              context.Memos.Add(memo); //error 1 here 
          } else {
              Memo dbEntry = context.Memos.Find(memo.MemoId);
              if(dbEntry != null) {
                  dbEntry.Message = memo.Message;
                  dbEntry.Employees = memo.Employees;
              }
          }
      context.SaveChanges(); //error 2 here
      }
  }
  </code></pre>
  
  <p>Error 1 I get when inserting:</p>
  
  <blockquote>
    <p>An entity object cannot be referenced by multiple instances of
    IEntityChangeTracker.</p>
  </blockquote>
  
  <p>Error 2 I get when updating:</p>
  
  <blockquote>
    <p>The relationship between the two objects cannot be defined because
    they are attached to different ObjectContext objects.</p>
  </blockquote>
  
  <p>How can I resolve this, I read topics about people saying it has something to do with using different contexts, others say it has something to do with <code>Attach()</code>, but I have no clue how to resolve this within my application.</p>
  
  <p>Please, tell me if you require more information.</p>
  
  <p>Note: I left out some code, to make it easier to read. Code can be added if requested of course.</p>