enashnash
enashnash

Reputation: 1578

Object from LINQ-to-SQL before SubmitChanges

I originally posted this as a response to this question but realised I should have asked a new one. It seems to suggest I should be able to do the following:

int count = Db.Countries.Count();
Country newCountry = new Country{Name = "France"};
Db.Countries.InsertOnSubmit(c);
Country getCountry = Db.Countries.FirstOrDefault(x => x.Name == "France");
count = Db.Countries.Count();
Db.Countries.DeleteOnSubmit(c);
count = Db.Countries.Count();

However, on an empty table, count remains 0 throughout while stepping through in the debugger and getCountry is null after execution.

What am I missing?

Upvotes: 2

Views: 452

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063298

LINQ-to-SQL sits in that nether-world between data in the database and objects held locally. At that point, you haven't submitted the changes to the database (Db.SubmitChanges()) - they only exist locally.

Db.Countries.Count() is executed at the database (i.e. select COUNT(1) from Countries) - and so the answer is 0.

It would be insanely hard (in the general case) to attempt to marry up the local deltas against the database world, so in short: don't do that. It won't work.

Upvotes: 2

Related Questions