嗨,我有表称之为产品......与列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