3

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.

Russell Steen
  • 6,494
  • 6
  • 38
  • 56

4 Answers4

4

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)

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

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.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
2

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
  • 524,688
  • 99
  • 697
  • 795
1

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
  • 6,494
  • 6
  • 38
  • 56