2011-02-16 127 views
1

我有這個事情:爲什麼dataContext.GetChangeSet()。Deletes.Count()總是返回0?

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

爲什麼?那麼刪除customers的次數的「正確」方法是什麼?

回答

3

因爲您已經提交了更改。一旦對SubmitChanges進行了調用,則不會刪除,因此(正確)返回零計數。要獲得刪除的項目的數量(或者被刪除),那麼試試這個(即確定計數推動改變之前):

... 

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

... 
+0

人力資源管理,如果由於某種原因在變更集中的客戶不會被刪除(也許有一些數據庫約束阻止了這一點)有無論如何來確定? – 2011-02-16 00:36:59

0

請問您的SubmitChanges方法提供了一個覆蓋,需要一個回調?

我已經工作了一下使用Silverlight和RIA服務,以及DomainContext的的SubmitChanges方法的形式爲SubmitChanges(Action<SubmitOperation>, object userState)

我們稱之爲如下的控制裝置:

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