8

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 (???)

}
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

2 Answers2

15

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
  • 111,873
  • 86
  • 233
  • 325
2

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
  • 81,193
  • 14
  • 123
  • 132
  • 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 Feb 25 '09 at 15:28
  • 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 Feb 25 '09 at 15:30
  • 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 Feb 25 '09 at 15:33
  • Also make sure your output window has "Show output from: " selected on Debug – Quintin Robinson Feb 25 '09 at 15:34
  • tried this: db.Log = new System.IO.StreamWriter(@"App_Data\linq.log") { AutoFlush = true } but it doesn't create a file. Hmm. – Edward Tanguay Feb 25 '09 at 16:06
  • 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 Feb 25 '09 at 16:07
  • 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 Feb 25 '09 at 16:14