title: How to implement Aggregate Root repository an add child entity with EF
body: <p>I'm developing an MVC application. I have a Domain Model, and I use a repositry pattern for data access and Entity Framework Code First. I also have a UnitOfWork class which I call the repository operations through.</p>
  
  <p>My problem mainly arises when I try to take advantage of aggregate roots and handle child objects through their parent repository.</p>
  
  <p>This is the problem:
  Parent class "Supplier" has several Contracts with Departments. I've chosen to make the contract a child of Supplier in this case.</p>
  
  <p>To add a new contract I need to add a method to add a contract in my SupplierRepository, I tried:</p>
  
  <pre><code> public class SupplierRepository : GenericRepository&lt;Supplier&gt; 
          {
  
              public SupplierRepository(MyContext context) 
              : base(context)
              {
              }
  
              public void AddSupplierContract(SupplierContract contract)
              {
                   var supplier = context.Suppliers.Find(contract.SupplierId);
                   supplier.Contract.Add(contract);
  
              }
  </code></pre>
  
  <p>And I also tried:</p>
  
  <pre><code>            public void AddSupplierContract(SupplierContract contract)
              {
                  context.Entry(contract).State = EntityState.Added;
              }
  
           }
  </code></pre>
  
  <p>When i call</p>
  
  <pre><code>_unitOfWork.save();
  </code></pre>
  
  <p>I get an error telling me: </p>
  
  <blockquote>
    <p>An entity object cannot be referenced by multiple instances of IEntityChangeTracker</p>
  </blockquote>
  
  <p>UnitOfWork instansiates my DbContext (myDbContext) and my SupplierRepository and calls the myDbContext.Save()</p>
  
  <ol>
  <li>Why do I get this behavior </li>
  <li>How should I implement an Aggregate Root Repository (CRUD operations for the child objects)</li>
  </ol>
  
  <p>As far as I understand I should have a method in the repository that takes a contract and adds it, and not do this in the Controller of my MVC app, but I don't seem to get it working.</p>
  
  <p>I've seen a lot of information about Aggregate Roots, but no examples of how to implement it.</p>
  
  <p>Thanks.</p>
  
  <p><strong>Solution:</strong></p>
  
  <p>Well I finally figured it out.</p>
  
  <p>So it was not a problem with the repository, but the new SupplierContract queryed the store for the user entity that created it (through an extension method). Obviously this context did not dispose and therefore i had two current DbContexts when I instansiated it to save the contract entity.</p>
  
  <p>Hopefully someone saves time by reading this.</p>
  
  <p>The Aggregate Root repository I solved by just doing like this in the SupplierRepository:</p>
  
  <pre><code>    public void AddSupplierContract(SupplierContract contract)
      {
          db.SupplierContracts.Add(contract);
      }
  </code></pre>
  
  <p>And calling UnitOfWork.Save() method.</p>