views:

109

answers:

4

Running .net 2008, asp.net, Linq to sql, sql server 2008 express

Foo f = db.Foo.First(x => x.ID == id);
f.IsValid = true;
db.SubmitChanges();

I have a very basic table, which has several fields, and no FK constraints. None of the data saves when I call .SubmitChanges(), but no error is thrown. Any ideas?

Not using any explicit transaction scope, fields are not autogenerated, and no triggers on the server.

+2  A: 

You need to set IsValid on f and not on x.

Foo f = db.Foo.First(x => x.ID == id); 
f.IsValid = true; 
db.SubmitChanges(); 

And remember to call Complete on your TransactionScope if you are using one.

klausbyskov
that was a typo. x.IsValid wouldn't compile, much less not throw an exception ;) Thanks for the catch
Russell Steen
+2  A: 

Is IsValid marked as an AutoGenerated field in the designer? If so, no changes that you make will be propagated back to the server. Also, is it possible that you have a DB trigger that may be running on update, instead of say on insert, that is setting it to a default value of 0 (false)?

tvanfosson
No triggers on the server.
Russell Steen
+2  A: 

Tyop aside, the first thing to try is setting db.Log = Console.Out; to see if any TSQL is issues (or better: run a SQL trace). If nothing is getting sent then maybe IsValid was already true - it won't re-save such changes unless it has to (unless you force it).

Less likely, you could perhaps have accidentally turning off object-tracking (db.ObjectTrackingEnabled).

Other common mistakes include:

  • not calling Complete() when using TransactionScope (if you are using TransactionScope)
  • with database files (rather than a separate server), checking in the original file (in the solution) rather than in the build output (typically /bin/debug or /bin/release).

I'm also assuming that IsValid is actually a db-mapped property (they don't have to be)

Marc Gravell
db.Log = Console.Out; is a superb tip, thank you.
Russell Steen
Marking this as the solution, since it led me to the solution.
Russell Steen
A: 

Turns out that the primary key had been dropped from the DB since the last time I was working with this application. When I regenerated the .dbml recently it generated with no primary key. Adding that back in has fixed the problem.

Russell Steen