Why is dataContext.GetChangeSet().Deletes.Count()

2019-07-25 07:08发布

问题:

I've got this going on:

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

Even when customers are successfully deleted deletedCount will be 0.

Why? And what would be the "correct" way of counting the number of customers deleted?

回答1:

Because you've submitted the changes already. Once the call to SubmitChanges has been made then there are no deletes, and therefore (correctly) a count of zero is returned. To get the number of items deleted (or, to be deleted) then try this (i.e. determining the count prior to pushing the changes):

...

var deletedCount = data.GetChangeSet().Deletes.Count();
data.SubmitChanges();

...


回答2:

Does your SubmitChanges method provide an override that takes a callback?

I've worked a bit with Silverlight and RIA Services, and the DomainContext's SubmitChanges method has an override of the form SubmitChanges(Action<SubmitOperation>, object userState)

We call it as follows:

mycontext.SubmitChanges( submitOperation => 
{
     //here, inside the callback, you can inspect
     //the submitOperation for the results
     //of the SubmitChanges method
}, null);