【发布时间】:2018-04-25 05:57:33
【问题描述】:
我正在使用 Linq 在 C# 中制作一个 Windows 窗体应用程序。我正在尝试更新外键列,但我不断收到以下错误:
“由于对象的当前状态,操作无效。”
错误的消息框指向该行:
throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
我已经使用以下方法尝试更新:
//Updates a Product
public void UpdateProduct(int productID, string productName, int categoryID, int supplierID, bool priceType, decimal costPrice, decimal retailPrice, bool inStock)
{
Product product = (from p in db.Products
where p.ProductId == productID
select p).Single();
product.Name = productName;
product.CategoryId = categoryID;
product.SupplierId = supplierID;
product.PriceType = priceType;
product.CostPrice = costPrice;
product.RetailPrice = retailPrice;
product.Stock = inStock;
db.SubmitChanges();
}
有什么问题? 提前谢谢!
【问题讨论】:
-
违规的代码行是 db.SubmitChanges() 吗?
-
没有。违规行是 product.CategoryId = categoryID;(表中的外键列) product.SupplierId = SupplierID;(表中的外键列)
标签: c# entity-framework linq