tags:

views:

878

answers:

2

I created a .MDF database in my WPF application.

I then generated LINQ-to-SQL classes and used LINQ get all the customers.

Then I run through them and change each of their last names.

However, when I call SubmitChanges, the database remains unchanged.

I thought that was the purpose of SubmitChanges(), to submit changes to the database?

What am I missing, how do I "submit changes" back to my database?

public Window1()
{
    InitializeComponent();

    Main2DataContext _db = new Main2DataContext();
    var customers = from c in _db.Customers
                    select c;

    foreach (var customer in customers)
    {
        customer.LastName = "CHANGED lastname"; //ListBox shows changes 
    }

    _db.SubmitChanges(); //does NOT save to database (???)

}
+4  A: 

I think I know what the problem is. Are you using a local .mdf file? If that's the case, check your bin/debug folder. I bet you have a database in there with the changes. I think you have an .mdf file in your project that is getting copied to the bin/debug folder everytime you build. Therefore the changes are getting saved to the copy, and you're not seeing it reflected in the copy that resides directly in your project.

Micah
I've just run through this and it seems correct. Attach to the bin/debug copy of the MDF and you'll see the changes.
Jason Punyon
bingo, that was it! appreciate the tenacity everyone
Edward Tanguay
I only know because I've spend hours chasing this same thing before. Gald I could help.
Micah
+1  A: 

Make sure that your _db isn't getting reset to a new context at any point between the retrieval and change, if so well that is the problem. Also you can simplify your assignment of your datasource by doing TheListBox.ItemsSource = _db.Customers;. I have many places in current code where I am doing exactly what you describe and the changes are propogating fine. An additional suggestion, setup logging on your context (set the _db.Log to some writer, I generally use the Console.Out so I can watch changes in the output window while debugging) so you can see the changes that actually do occur when you call SubmitChanges().

Quintin Robinson
I put in _db.Log = Console.Out; before the submitchanges line, it doesn't show in my Output window, or where is it supposed to output?
Edward Tanguay
ok, I maximally simplified the code now, it just gets the objects, changes them and saves them back with SubmitChanges, but the database still doesn't change them. Any idea why this is the case?
Edward Tanguay
Are you running in debug mode? Also do you have the option "Redirect all Output Window text to the Immediate Window" checked in Tools->Options->Debugging->General ? If so your output is going to the immediate window, otherwise Try setting up your own TextWriter around a StringBuilder and set Log.
Quintin Robinson
Also make sure your output window has "Show output from: " selected on Debug
Quintin Robinson
tried this: db.Log = new System.IO.StreamWriter(@"App_Data\linq.log") { AutoFlush = true } but it doesn't create a file. Hmm.
Edward Tanguay
ok turned on "Redirect all Output Window text to the Immediate Window" but it just tells me that it is loading each dll hmm.
Edward Tanguay
Well it seems like that is a separate issue, since we can't narrow this down I would take a look at Micah's suggestion.
Quintin Robinson