Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LinqToSQL Not Updating the Database

// goal: update Address record identified by "id", with new data in "colVal"

string cstr = ConnectionApi.GetSqlConnectionString("SwDb"); // get connection str
using (DataContext db = new DataContext(cstr)) {
    Address addr = (from a in db.GetTable<Address>()
                    where a.Id == id
                    select a).Single<Address>();
    addr.AddressLine1 = colValue.Trim();
    db.SubmitChanges(); // this seems to have no effect!!!
}

In the debugger, addr has all the current values from the db table, and I can verify that AddressLine1 is changed just before I call db.SubmitChanges()... SQL Profiler shows only a "reset connection" when the SubmitChanges line executes. Anyone got a clue why this isn't working? THANKS!

like image 701
Steve L Avatar asked Jan 21 '26 21:01

Steve L


1 Answers

You can get a quick view of the changes to be submitted using the GetChangeSet method.

Also make sure that your table has a primary key defined and that the mapping knows about this primary key. Otherwise you won't be able to perform updates.

like image 75
Bryant Avatar answered Jan 24 '26 16:01

Bryant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!