嗨,我有表稱之爲產品......與列LINQ更新問題enitites
product_id (p.K)
product_name
product_description
product_price
category_id (F.k)
我還有一個表類
category_id (p.k)
category_name
我曾嘗試是我試圖更新與category_id產品表得到了問題 我得到了以下代碼....
Private void btnsave_click(object sender , eventargs e)
{
if (datagridview1.SelectedRows.Count > 0)
{
int updateproductid = Convert.ToInt32(datagridview1.SelectedRows[0].Cells[0].Value);
string productcategories =cbCategorytypes.Text;
var categorytypes = (from producttype in dbcontext.categories
where producttype.name.Equals(productcategories)
select producttype.categoryId).SingleOrDefault();
product product1 = new product() { productId = updateproductid };
dbcontext.products.Attach(product1);
product1.Name = txtProductname.Text;
product1.Description = txtProductdescription.Text;
product1.Price = Convert.ToDecimal(txtProductPrice.Text);
product1.categoryId = categorytypes;
dbcontext.SaveChanges();
}
}
出錯:無效的操作異常未處理:ObjectStateManager中已存在具有相同鍵的對象。 ObjectStateManager不能使用同一個鍵跟蹤多個對象。
將在此任何一個請幫助.....
非常感謝....
主鍵應設置爲身份= TRUE,種子= 1 – Alleo
我在哪裏可以在數據庫中設置這些的... – rockyashkumar
在列的性質。 http://2.bp.blogspot.com/_s2jU7girbyM/TQrbPX_T7EI/AAAAAAAAC6Q/f_1OuWD5fHE/s1600/SQL_2008_Unable_To_Change_Id_Spec.PNG – Alleo