【发布时间】:2011-11-09 15:09:03
【问题描述】:
您好,我有一个名为 products...with 列的表
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 无法跟踪具有相同键的多个对象。
有没有人帮忙解决这个问题.....
非常感谢....
【问题讨论】:
-
主键应该设置为identity = true, seed = 1
-
我在哪里设置这些...
标签: c# winforms linq linq-to-entities entity-framework-4.1