【发布时间】:2017-10-03 03:56:32
【问题描述】:
我在这段代码中遇到了这个错误:
Unable to update the EntitySet 'Ingredient_Quantity' because it has a
DefiningQuery and no <InsertFunction> element exists in the
<ModificationFunctionMapping> element to support the current operation.
代码。
Ingredient ing = new Ingredient();
ing.name = ingredientVM.name;
ing.Ingredient_Type_id = ingredientVM.typeId;
ing.UOM_id = ingredientVM.uomId;
ing.is_deleted = 0;
db.Ingredients.Add(ing);
db.SaveChanges();
int latestIngredientId = ing.id;
Ingredient_Quantity iq = new Ingredient_Quantity
{
Ingredient_id = latestIngredientId,
quantity_as_of = DateTime.Now,
quantity = (double)ingredientVM.quantity
};
db.Ingredient_Quantity.Add(iq);
db.SaveChanges(); // HERE IS WHERE I'M GETTING THE ERROR
从我在互联网上看到的情况来看,这是因为我的Ingredient_Quantity 将每一列都视为实体键。我不知道这是否正确。
我该如何改变呢?有什么建议吗?
【问题讨论】:
-
我认为您需要在
Ingredient_Quantity数据库表中设置一列作为主键。尝试将主键添加到Ingredient_id,删除实体并重新添加它。Ingredient_Quantity是不是一个视图? -
你的意思是像this? “'Ingredient_Quantity' 是一个视图”是什么意思?什么是实体映射?对不起,我真的很陌生。
-
是的,一个这样的主键。该设置现在有效吗?我只想问
Ingredient_Quantity是数据库表还是数据库视图,它们在EF实体映射中使用时有不同的处理方式。 -
当我尝试将
Ingredient_id设为主键时出现错误,它具有多重性或其他东西。所以我也尝试让quantity_as_of成为主键。但它仍然不起作用。Ingredient_Quantity是一个数据库表 -
所以你想说
Ingredient_Quantity在Ingredient中有外键?我认为您有像Multiplicity conflicts with the referential constraint这样的错误,如果发生这种情况,请尝试将外键设置为可空类型并将关系设置为 0/1..n(零/一对多)。
标签: asp.net-mvc entity-framework-6