我有这个代码:
var newProduct = new Product();
newProduct.Name = "product name";
newProduct.Description = "product descvription";
ProductImpl.Insert(newProduct);
var ingredient1 = new Ingredient { Description = "ingredient 1" };
var ingredient2 = new Ingredient { Description = "ingredient 2" };
var ingredient3 = new Ingredient { Description = "ingredient 3" };
IngredientImpl.Insert(ingredient1);
IngredientImpl.Insert(ingredient2);
IngredientImpl.Insert(ingredient3);
newProduct.Ingredients.Add(ingredient1);
newProduct.Ingredients.Add(ingredient2);
newProduct.Ingredients.Add(ingredient3);
ProductImpl.Update(newProduct);
当我写 newProduct.Ingredient.Add 它不会向成分表中添加新成分时,我该怎么办?因为当我这样做时,我的配料表将有六个。
成分Impl.Insert:
public void Insert(Ingredient ingredient)
{
Validate(ingredient);
_repository.Insert(ingredient);
}
存储库:
public void Insert(Ingredient ingredient)
{
db.Ingredient.Add(ingredient);
db.SaveChanges();
}
产品说明:
public void Update(Product produto)
{
Validate(produto);
_repository.Update(produto);
}
存储库:
public void Update(Product product)
{
db.Entry(product).State = EntityState.Modified;
SaveChanges();
}