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.

0

4 Answers 4

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Marking this as the solution, since it led me to the solution.
@Marc this issue can also be caused by the table not having a PK defined. In this case LINQ to SQL will silently ignore changes.
And for those who using Entity Framework 6 db.Database.Log = Console.Write
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.

1 Comment

that was a typo. x.IsValid wouldn't compile, much less not throw an exception ;) Thanks for the catch
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)?

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.