title: Entity Framework Update method to object with collection
body: <p>I have problem with updating existing entity in DB. I'm using Entity Framework Code First.</p>
  
  <p>I have small program with calculation food.</p>
  
  <p>My domain classes></p>
  
  <pre><code>public class Unit
      {
          public int Id { get; set; }
          public string Name { get; set; }
      }
  
  public abstract class Item
      {
          public int Id { get; set; }
          public string Name { get; set; }
      }
  
  public class Ingredient : Item
      {        
          public virtual Unit Unit { get; set; }               
          public decimal Price { get; set; }        
      }
  
  public class Recipe : Item 
      {
          public virtual List&lt;RecipeItem&gt; Items { get; set; }
          public double WeightCoocked { get; set; }
          public bool IsAlsoIngredient { get; set; }        
      }
  
  public class RecipeItem
      {
          public int Id { get; set; }
          public virtual Item Item { get; set; }        
          public int Quantity { get; set; }
      }
  </code></pre>
  
  <p>Note only that RecipeItem can be Ingredient but also Recipe, so I can include recipe into recipe.</p>
  
  <p>I tried this kind of updates methods:</p>
  
  <pre><code>public void Update1(Recipe recipe)
          {            
              if (recipe == null) 
                  return;                
  
              var originalRecipe = DataContext.Recipes.FirstOrDefault(i =&gt; i.Id == recipe.Id);
  
              if (originalRecipe == null) 
                  return; 
  
              DataContext.Entry(originalRecipe).CurrentValues.SetValues(recipe);
  
              DataContext.SaveChanges();
          }
  </code></pre>
  
  <p>Update1 works, but my collection is not updated.</p>
  
  <pre><code>public void Update2(Recipe recipe)
          {            
              if (recipe == null) 
                  return;                
  
              var originalRecipe = DataContext.Recipes.FirstOrDefault(i =&gt; i.Id == recipe.Id);
  
              if (originalRecipe == null) 
                  return; 
  
              DataContext.Entry(originalRecipe).State = EntityState.Modified;
  
              DataContext.SaveChanges();
          }
  </code></pre>
  
  <p>Update1 works same like Update1 but if I change collection I got exception.
  I also tried put there this line</p>
  
  <pre><code>DataContext.Entry(originalRecipe.Items).State = EntityState.Modified;
  </code></pre>
  
  <p>Last Update method></p>
  
  <pre><code>public void Update3(Recipe recipe)
          {            
              if (recipe == null) 
                  return;                
  
              var originalRecipe = DataContext.Recipes.FirstOrDefault(i =&gt; i.Id == recipe.Id);
  
              if (originalRecipe == null) 
                  return; 
  
              DataContext.Recipes.Attach(recipe);
              DataContext.Entry(originalRecipe).State = EntityState.Modified;
              DataContext.Entry(originalRecipe.Items).State = EntityState.Modified;
  
              DataContext.SaveChanges();
          }
  </code></pre>
  
  <p>Getting same exception:
  An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.</p>
  
  <p>I have no problem with updating tables> Units and Ingredients but with collection makes me nervous. I already watched vidoes about EF on pluralsight but there is not example like I have.</p>
  
  <p>Thank for your help.</p>