2013-04-06 178 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