之前的SubmitChanges从LINQ到SQL对象

问题描述:

我最初发布此作为回应这个问题但意识到我应该问一个新的。这似乎在暗示我应该能够做到以下几点:

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();

然而,在一个空表,计数仍然 0 贯穿而在调试器步进通过与 getCountry 执行后。

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

我在想什么?

LINQ到SQL坐落在本地保存在数据库中的数据和对象之间的幽冥世界。在这一点上,你还没有提交的数据库更改( Db.SubmitChanges()) - 它们只存在于本地

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()是在数据库中执行(即 SELECT COUNT(1)从国家 ) - 所以答案是0

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.