【发布时间】:2021-12-10 19:05:26
【问题描述】:
当数据已经存在时,我正在尝试创建程序,它将更新数据,如果没有,那么它将添加数据,下面是我的代码.. 更新工作但添加不起作用。如果我删除更新代码,那么添加将起作用
var itemLocQuantity = objERPMicroDbEntities.ItemLocationDatas.FirstOrDefault(items => items.ItemId == item.ItemId && items.LocationId == item.LocationId);
if (itemLocQuantity.ItemId == item.ItemId && itemLocQuantity.LocationId == item.LocationId) {
itemLocQuantity.Quantity += item.QuantityReceive;
}
以上是更新
if (itemLocQuantity == null)
{
itemLocQuantity = new ItemLocationData();
// create new in db
itemLocQuantity.ItemId = item.ItemId;
itemLocQuantity.Quantity = item.QuantityReceive;
itemLocQuantity.LocationId = item.LocationId;
objERPMicroDbEntities.ItemLocationDatas.Add(itemLocQuantity);
}
以上是尝试添加的代码,但我的程序在尝试添加新数据时返回错误
【问题讨论】:
-
你能解释一下错误是什么
标签: c# asp.net entity-framework model-view-controller entity-framework-core