2013-04-06 179 views
0
public bool UpdateValues(String impR, String actR, String proR, String impV, String magV) 
{ 
     bool IsInserted = false; 

     try 
     { 
      MatrixValues c = cecbContext.MatrixValues.First(i => i.actv_reference == actR); // primary key 
      c = cecbContext.MatrixValues.First(i => i.impt_reference == impR); // primary key 
      c = cecbContext.MatrixValues.First(i => i.proj_reference == proR); // primary key 

      c.mtrxV_importance = double.Parse(impV); // updated value 
      c.mtrxV_magnitude = double.Parse(magV); // updated value 

      cecbContext.SaveChanges(); // getting an error here!!! 

      IsInserted = true; 
     } 
     catch (Exception) 
     { 
      IsInserted = false; 
     } 

     return IsInserted; 
    } 

實體框架的更新語句試圖更新細節導致錯誤

錯誤時,我得到一個錯誤是PRIMARY KEY約束「PK_MatrixValues」的

衝突。無法在對象'dbo.MatrixValues'中插入重複鍵。

回答

1

您正在設置對象c多次;如果最後的陳述是足夠的話;那麼不要使用以前的;如果要使用多個標準選擇c對象,則需要更改以下幾行;

MatrixValues c = cecbContext.MatrixValues.First(i => i.actv_reference == actR); // primary key 
    c = cecbContext.MatrixValues.First(i => i.impt_reference == impR); // primary key 
    c = cecbContext.MatrixValues.First(i => i.proj_reference == proR); // primary key 

到:

MatrixValues c = cecbContext.MatrixValues.First(i => i.actv_reference == actR && c.impt_reference == impR && c.proj_reference == proR); 
0

違反PRIMARY KEY約束'PK_MatrixValues'。不能在對象中插入重複密鑰'dbo.MatrixValues'

這意味着該字段包含主鍵。它將不允許重複輸入

+1

你能告訴我如何更新價值觀,我不想更新主鍵,我想更新只有兩個值的字段 – Gayashan 2013-04-06 05:57:33