Jonas Stawski

Everything .NET and More

Linq To Sql not updating on Context.SubmitChanges()

Tonight I ran into a weird problem that I haven’t seen before. I was getting an entity back from a query, changing some values, and calling the DataContext.SubmitChanges() and the database was not being updated. The code ran fine without any exceptions. Weird! I decided to fire up SQL Server Profiler and see what queries were being executed and to my surprise Linq To Sql (L2S) was not executing anything. After 5 minutes of thinking why that would be the case the light bulb turned on! I didn’t have a Primary Key set on the table. Why I didn’t have one it’s not important other than this table didn’t really need it as far as SQL Server is concerned. It turns out that L2S does need one in order to generate updates.

Happy Programming!

Comments (3) -

Thanks for this post! I ran into the same problem tonite and couldn't figure out what was happening. Your post provided great help. Infact, when I read your post, I made the correction in the database but forgot to update the designer so it didnt work. Its only when I set the primary key in the designer that it worked.  Thanks a lot.

Reply

I did another test. I removed the primary key from the column in the table but kept the primary key set on the column in the designer. The update worked!

Reply

see the context that you are using while updating.
Context should be the same while fetching to update the row.

Reply

Add comment

biuquote
Loading