Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Linq -- SubmitChanges() not saving to database

I've been working with Linq now for a while, but sometimes the simplest things get the best of you.  I was working with a new project where I had been using Linq all over the place. Everything was working, selecting rows, inserting logs, etc, etc, however, one portion was not. I was attempting to get a row from the database, changing a value then saving back that row. 

Here's an example:

DataContext db = new DataContext(pConnectionString);

MyRow pRow = db.MyRows.SingleOrDefault(p => p.pkID == ppkID);

pRow.column = DateTime.Now;

db.SubmitChanges();

Looked good to me.  So I did some digging through my database and found that for some reason I did not set a Primary Key on the table I was trying to update. There aren't any exceptions or errors thrown so it was a little more hidden.

So to summarize, make sure you set your primary keys when using linq, even if it needs to be a surrogate key!

~ Steve

 

 

No Comments