tags:

views:

82

answers:

2

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
        }

        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.

A: 

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?

popester
No, the result set has not been updated according to the debugger. However, according to Scott's blog (and he is a Corporate VP in Microsoft's Developer Division, so I would hope he knows what he's talking about), this pattern should work. If this isn't the correct pattern, what is?
Eric J.
@Eric J. - It is the right pattern. We use this all over our application and it works fine.
Mike Two
+1  A: 

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.

Mike Two
The class with ProcessCode is generated by Linq. I don't know if the generated class implements INotifyPropertyChange.
Eric J.
@Eric J. It should.
Mike Two
I saw the question about having a primary key. However, I do have one. It's defined as a unique identifier rather than an int, but there is a PK.
Eric J.
The ChangeSet is empty. ObjectTrackingEnabled is true.
Eric J.
SOLVED! The PK was not registered with Linq somehow. I removed the relevant table from the designer and re-added it, and the updates started working. Thanks for the help.
Eric J.