DB.SubmitChanges() does not update data

February 12, 2010

I’ve just fixed a bug where a record was not updating as expected via a LINQ 2 SQL expression. Having checked the code, it was the usual method – Connect to the database, get the object, change the properties, submit the new values to the database – All looked well and good, but the new values were not appearing in the database.

Having checked my connection string to ensure I was connecting to the right database, and also checking that I was viewing the same database in SQL Server management studio when checking the value, some research was in order.

Turns out the LINQ2SQL class was missing the primary key (even though the SQL table had one set). In this situation, LINQ ignores the update and does not change the data. I’m not sure how the PK was lost from the class in the first place, but deleting and re-adding the table to the LINQ schema solved the problem – After doing this the PK was in place.

My only outstanding question with this: If I’m calling ‘SubmitChanges’ clearly I’m wanting to update some values and LINQ knows what classes (Tables) I’m working with at the time – So why is there no check to ensure that a PK is in place – Which should then result in an exception being thrown…answers on a comment please !

3 Responses to “DB.SubmitChanges() does not update data”

  1. AnhQuan said

    Very Good , Thank For Your Experience!!!

  2. Great friend, I fixed bug in my project. I experienced the same.

Leave a comment