开发者

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 == "开发者_如何学JAVAFrance");
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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜