C# 为什么dataContext.GetChangeSet().Deletes.Count()总是返回0?
我有这样的想法:C# 为什么dataContext.GetChangeSet().Deletes.Count()总是返回0?,c#,asp.net,linq,linq-to-sql,C#,Asp.net,Linq,Linq To Sql,我有这样的想法: var customerIdsToDelete = new {1, 2, 3}; var dataContext = new DataContext(); var customersToDelete = (from c in data.Customers where customerIdsToDelete.Contains(c.CustomerID) select c); da
var customerIdsToDelete = new {1, 2, 3};
var dataContext = new DataContext();
var customersToDelete = (from c in data.Customers
where customerIdsToDelete.Contains(c.CustomerID)
select c);
data.Customers.DeleteAllOnSubmit(customersToDelete);
data.SubmitChanges();
var deletedCount = data.GetChangeSet().Deletes.Count();
即使成功删除客户,deletedCount
也将为0
为什么??计算被删除的
客户数的“正确”方法是什么?因为您已经提交了更改。一旦调用了SubmitChanges
,则不存在删除,因此(正确地)返回零计数。要获取已删除(或要删除)的项目数,请尝试此操作(即在推送更改之前确定计数):
SubmitChanges方法是否提供接受回调的重写
我曾经使用过Silverlight和RIA服务,DomainContext的SubmitChanges方法覆盖了表单SubmitChanges(Action,object userState)
我们称之为:
mycontext.SubmitChanges( submitOperation =>
{
//here, inside the callback, you can inspect
//the submitOperation for the results
//of the SubmitChanges method
}, null);
hrm,如果由于某种原因更改集中的某个客户没有被删除(可能有一些数据库约束阻止了这一点),是否仍有其他方法来确定?是的,您的应用程序将遇到异常,假设这在异常情况下会发生。
mycontext.SubmitChanges( submitOperation =>
{
//here, inside the callback, you can inspect
//the submitOperation for the results
//of the SubmitChanges method
}, null);