Linq not updating changed class property
First of all, I have read the similar posts and don't see how they solve this problem. If I'm missing something in them, please point out what.
My Linq code is very similar to Scott Gu's expensiveUnpopularProducts example. However, in my case, the database is not being updated with the new value. There are no exceptions raised, and all of the variables seem to have reasonable values in the debugger (result set populated, connection string correct, ...).
using (MyDataContext db =
new MyDataContext(CONNECTION_STRING))
{
var resultSet = from l in db.Logs
where l.ProcessCode == null
select l;
foreach (var bm in resultSet)
{
bm.ProcessCode = 1;
// Debugger shows bm.ProcessCode properly set
}
开发者_JAVA百科 db.SubmitChanges();
}
Why might SubmitChanges() not cause the DB to be updated?
NOTE: This is a simplified case of my real method. The real one also inserts records into another table. That insert is working, so I'm sure the connection string is correct and functioning.
EDIT: See the answer to this question. It might be the answer for you too. The answer turned out to be that LINQ to SQL will not do updates unless there is a primary key on the table. I bet you also need to set the primary key parts of the ColumnAttribute
on the class. Since you have used the code generator you might need to regenerate that part after updating the table in the database. Assuming that is the problem of course.
Does the class with the ProcessCode
property implement INotifyPropertyChanged
? And does the ProcessCode
property fire the PropertyChanged
event?
You also need to make sure the DataContext
has it's ObjectTrackingEnabled
property set to true. It should be by default, but it is easy to check.
You can also use the GetChangeSet
method on the DataContext
to see what the updates are. It could help with debugging.
My first guess is that your Linq query performs a deep-copy into resultSet of any matching elements. As such, when you do your db.SubmitChanges() you're operating on another copy of data. If you put a breakpoint right before db.SubmitChanges() can you see that the data in db is correctly updated?
精彩评论