title: Adding items to a collection using Entity Framework
body: <p>I'm trying to follow the DDD Repository pattern with Entity Framework 4. But I'm having problems saving changes to collection properties of my aggregate roots. Consider my classes below. Item is my aggregate root which contains a collection of SubItem entities.</p>
  
  <pre><code>public class Item
  {
      public int ItemId { get; set; }
      public string Name { get; set; }
      public ICollection&lt;SubItem&gt; SubItems { get; private set; }
  
      public Item()
      {
          this.SubItems = new HashSet&lt;SubItem&gt;();
      }
  }
  
  public class SubItem
  {
      public int ItemId { get; set; }
      public int SubItemId { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Next I defined a repository interface for my aggregate root class</p>
  
  <pre><code>public interface IItemRespository
  {
      Item Get(int id);
      void Add(Item i);
      void Save(Item i);
  }
  </code></pre>
  
  <p>Now here is my DbContext class that sets up the EF mapping.</p>
  
  <pre><code>public class ItemContext : System.Data.Entity.DbContext
  {
      protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Item&gt;().HasKey(i =&gt; i.ItemId);
          modelBuilder.Entity&lt;Item&gt;().Property(i =&gt; i.Name);
  
          modelBuilder.Entity&lt;Item&gt;().HasMany(i =&gt; i.SubItems)
              .WithRequired()
              .HasForeignKey(si =&gt; si.ItemId);
  
          modelBuilder.Entity&lt;SubItem&gt;().HasKey(i =&gt; i.SubItemId);
          modelBuilder.Entity&lt;SubItem&gt;().Property(i =&gt; i.Name);
      }
  }
  </code></pre>
  
  <p>Finally here's my implementation of IRepository using the DBContext</p>
  
  <pre><code>public class Repository : IItemRespository
  {
      public void Save(Item i)
      {
          using (var context = new ItemContext())
          {
              context.Set&lt;Item&gt;().Attach(i);
              context.SaveChanges();
          }
      }
  
      public Item Get(int id)
      {
          using (var context = new ItemContext())
          {
              var result = (from x in context.Set&lt;Item&gt;() where x.ItemId == id select x).FirstOrDefault();
              return result;
          }
      }
  
      public void Add(Item i)
      {
          using (var context = new ItemContext())
          {
              context.Set&lt;Item&gt;().Add(i);
              context.SaveChanges();
          }
      }
  }
  </code></pre>
  
  <p>The following code creates a new Item, adds it to the repository, adds some new SubItems, and then saves the changes.</p>
  
  <pre><code>IItemRespository repo = new Repository();
  
  //Create a new Item
  Item parent = new Item() { Name = "Parent" };
  repo.Add(parent);
  
  //A long period of time may pass .. . .
  
  //later add sub items
  parent.SubItems.Add(new SubItem() { Name = "Child 1" });
  parent.SubItems.Add(new SubItem() { Name = "Child 2" });   
  parent.SubItems.Add(new SubItem() { Name = "Child 3" });
  
  //save the added sub items
  repo.Save(parent);
  </code></pre>
  
  <p>I get the following exception when the Save() method tries to attach the item to the context.</p>
  
  <blockquote>
    <p>A referential integrity constraint violation occurred: The property
    values that define the referential constraints are not consistent
    between principal and dependent objects in the relationship.</p>
  </blockquote>
  
  <p>I realized I'm creating a new context for each method in the repository. This is intentional. A long period of time may pass between when an Item is added and then later edited, and I don't want to keep the context or database connection open for the entire time.</p>
  
  <p>Now if I attach the newItem to the second context before adding the sub items as in the code below it works. </p>
  
  <pre><code>//Create a new item
  Item newItem = new Item() { Name = "Parent" };
  
  using (ItemContext context1 = new ItemContext())
  {
      //Create a new aggrgate
      context1.Set&lt;Item&gt;().Add(newItem);
      context1.SaveChanges();
  }
  
  //Long period of time may pass
  
  using (ItemContext context2 = new ItemContext())
  {
      context2.Set&lt;Item&gt;().Attach(newItem);
  
      newItem.Name = "Edited Name";
      newItem.SubItems.Add(new SubItem() { Name = "Child 1" });
      newItem.SubItems.Add(new SubItem() { Name = "Child 2" });
      newItem.SubItems.Add(new SubItem() { Name = "Child 3" });
  
      context2.SaveChanges();
  }
  </code></pre>
  
  <p>However, if I want to be true to the repository pattern the code that edits Item shouldn't know anything about how the repository works or the ItemContext class. It should simply be able to make changes to an aggregate root entity, and then save those changes through a repository Save() method.</p>
  
  <p>So how do I modify my Save() method so that changes to Item.SubItems are saved correctly?</p>