2011-09-03 20 views
1

嗨,我有表称之为产品......与列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不能使用同一个键跟踪多个对象。

将在此任何一个请帮助.....

非常感谢....

+0

主键应设置为身份= TRUE,种子= 1 – Alleo

+0

我在哪里可以在数据库中设置这些的... – rockyashkumar

+0

在列的性质。 http://2.bp.blogspot.com/_s2jU7girbyM/TQrbPX_T7EI/AAAAAAAAC6Q/f_1OuWD5fHE/s1600/SQL_2008_Unable_To_Change_Id_Spec.PNG – Alleo

回答

3

你所得到的错误,因为你要更新的产品已经通过实体框架加载。您正在创建product的新实例并分配现有的product id

您可以使用dbcontext.products DbSet的Local属性检索现有产品。

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 = dbcontext.products.Local.Where(p => p.productId == updateproductid).First(); 
product1.Name = txtProductname.Text; 
product1.Description = txtProductdescription.Text; 
product1.Price = Convert.ToDecimal(txtProductPrice.Text); 
product1.categoryId = categorytypes; 
dbcontext.SaveChanges(); 

您应该考虑使用适当的命名约定

+0

非常感谢..... – rockyashkumar

1

此行

product product1 = new product() { productId = updateproductid }; 
dbcontext.products.Attach(product1); 

告诉我,你要创建一个新的产品和安装它的上下文。但是这个产品已经存在。您应该根据updateproductid检索产品并设置新的categoryId或您想要更改的属性。

更确切地说,你应该用什么取代

product product1 = new product() { productId = updateproductid }; 
dbcontext.products.Attach(product1); 

这样

product product1 = (from product in dbcontext.products 
        where productId == updateproductid select product); 
+0

我要做的事情你会建议我...... – rockyashkumar

+0

非常感谢您的支持。 – rockyashkumar

+0

@eranga之后,我可以像这样附加... product1.Name = txtProductname.Text; product1.Description = txtProductdescription.Text; product1.Price = Convert.ToDecimal(txtProductPrice.Text); product1.categoryId = categorytypes; dbcontext.SaveChanges(); – rockyashkumar