I have problem with updating existing entity in DB. I'm using Entity Framework Code First.
I have small program with calculation food.
My domain classes>
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<RecipeItem> 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; }
}
Note only that RecipeItem can be Ingredient but also Recipe, so I can include recipe into recipe.
I tried this kind of updates methods:
public void Update1(Recipe recipe)
{
if (recipe == null)
return;
var originalRecipe = DataContext.Recipes.FirstOrDefault(i => i.Id == recipe.Id);
if (originalRecipe == null)
return;
DataContext.Entry(originalRecipe).CurrentValues.SetValues(recipe);
DataContext.SaveChanges();
}
Update1 works, but my collection is not updated.
public void Update2(Recipe recipe)
{
if (recipe == null)
return;
var originalRecipe = DataContext.Recipes.FirstOrDefault(i => i.Id == recipe.Id);
if (originalRecipe == null)
return;
DataContext.Entry(originalRecipe).State = EntityState.Modified;
DataContext.SaveChanges();
}
Update1 works same like Update1 but if I change collection I got exception. I also tried put there this line
DataContext.Entry(originalRecipe.Items).State = EntityState.Modified;
Last Update method>
public void Update3(Recipe recipe)
{
if (recipe == null)
return;
var originalRecipe = DataContext.Recipes.FirstOrDefault(i => 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();
}
Getting same exception: An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
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.
Thank for your help.