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 ro开发者_JAVA技巧ws).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
Another thing to check: ensure the table has a primary key.
A couple of things to check:
- Is your code snippet using the same
DataContext
, meaning it's not cut and pasted from different methods where you're instantiating newDataContext
instances? - Is your
DataContext.ObjectTrackingEnabled
property set totrue
? It should be by default, but if it'sfalse
then yourDataContext
won't be able to perform the update.
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?
精彩评论