Jimmy
Jimmy

Reputation: 359

LINQ to SQL database row not updating

I am using LINQ to SQL to interact with my database and I am trying to do the following to update a single row in the database:

DataClassesDataContext dataContext = new DataClassesDataContext();

TableName aRow = (from rows in dataContext.TableNames where rows.x == y select rows).Single();
aRow.attribute = "something";

dataContext.SubmitChanges();

shouldn't this update the database with the change I made to the row? or is there something I'm missing?

thanks for any help

Upvotes: 3

Views: 2805

Answers (3)

Arran
Arran

Reputation: 25076

Another thing to check: ensure the table has a primary key.

Upvotes: 3

David Hoerster
David Hoerster

Reputation: 28711

A couple of things to check:

  1. Is your code snippet using the same DataContext, meaning it's not cut and pasted from different methods where you're instantiating new DataContext instances?
  2. Is your DataContext.ObjectTrackingEnabled property set to true? It should be by default, but if it's false then your DataContext won't be able to perform the update.

Upvotes: 1

bradenb
bradenb

Reputation: 793

That code should work correctly, so it must be elsewhere in the code or in the database itself.

Have you checked to make sure that the column you are updating is not read-only? As pst mentioned, are you using transactions?

Upvotes: 0

Related Questions