Linq ForeignKeyReferenceAlreadyHasValueException
我有一个名为Apuntes的SQL表: codigo蛋白激酶 名义 证明人FK 材料 科斯托 普里西奥 最低库存量 我在mi LINQ DAL层上得到了以下代码:Linq ForeignKeyReferenceAlreadyHasValueException,linq,Linq,我有一个名为Apuntes的SQL表: codigo蛋白激酶 名义 证明人FK 材料 科斯托 普里西奥 最低库存量 我在mi LINQ DAL层上得到了以下代码: public static Apuntes UpdateByCodigo(Apuntes LLevarDatos) { Apuntes Update = (from u in db.Apuntes where u.Codigo.Equals(LLevarDatos.Codigo)
public static Apuntes UpdateByCodigo(Apuntes LLevarDatos)
{
Apuntes Update = (from u in db.Apuntes
where u.Codigo.Equals(LLevarDatos.Codigo)
select u).FirstOrDefault();
//Update = LLevarDatos;
Update.Nombre = LLevarDatos.Nombre;
Update.Proveedor = LLevarDatos.Proveedor;
Update.Materia = LLevarDatos.Materia;
Update.Costo = LLevarDatos.Costo;
Update.Precio = LLevarDatos.Precio;
Update.StockMinimo = LLevarDatos.StockMinimo;
db.SubmitChanges();
return Update;
}
在我的Winforms层中:
string CUITPK = DAL_Apuntes_Stock.BuscaCUIT(cmbProveedor.Text).CUIT;
Apuntes LlevarDatos = new Apuntes();
LlevarDatos.Codigo = txtCodigo.Text;
LlevarDatos.Nombre = txtNombre.Text;
LlevarDatos.Proveedor = CUITPK;
LlevarDatos.Materia = cmbMateria.Text;
LlevarDatos.Costo = txtCosto.Text;
LlevarDatos.Precio = txtPrecio.Text;
LlevarDatos.StockMinimo = Convert.ToInt32(txtStockMinimo.Text);
DAL_Apuntes_Stock.UpdateByCodigo(LlevarDatos);
MessageBox.Show("OK", "info");
我收到的错误是
ForeignKeyReferenceAlreadyHasValueException in the following line:
LlevarDatos.Proveedor = CUITPK;
有人对此有什么想法吗?您可能需要分配实体,而不仅仅是值 如下所示:
//Assuming your Proveedor foreignKey is a string and not an int
LlevarDatos.Proveedor = yourDataSource.Proveedor.Single(x.Id == CUITPK);
你能在你的答案中贴出你的Apuntes类吗?