Reputation: 351
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;
}
I'm getting an error when trying to update details
Error is
Violation of PRIMARY KEY constraint 'PK_MatrixValues'. Cannot insert duplicate key in object 'dbo.MatrixValues'.
Upvotes: 0
Views: 112
Reputation: 14919
You are setting the object c
multiple times; if the last statement is enough; then do not use the previous ones; if you want to select the c
object using multiple criterias you need to change the following lines;
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
to:
MatrixValues c = cecbContext.MatrixValues.First(i => i.actv_reference == actR && c.impt_reference == impR && c.proj_reference == proR);
Upvotes: 1
Reputation: 10545
Violation of PRIMARY KEY constraint 'PK_MatrixValues'. Cannot insert duplicate key in object 'dbo.MatrixValues'
This means the field contains Primary Key. It won't allow duplicate entries
Upvotes: 0