2011-09-03 65 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