2010-11-20 117 views
1

HI, Wit linq我在DAL中填充通用列表。然後在UI層中,用戶從列表中刪除並更新項目。是否有可能讓LINQ跟蹤更改,而不必編寫代碼來檢查哪些項目已被刪除並刪除它們/添加新項目並更新其他項目等等。看起來,這在使用datacontext時不可能實現獨立的datacontext是負責SubmitChanges。任何幫助讚賞。LINQ跟蹤更改

感謝 尼爾

回答

3

如果您使用的是Linq.DataContext,那麼它確實會跟蹤您的變化。

要引用的函數是System.Data.Linq.DataContext.GetChangeSet。儘管可以在任何時候調用GetChangeSet,但最好在MSDN文檔中引用Remarks部分。

您可以檢查/採取提交操作...

這裏是你如何能覆蓋在DataContextSubmitChanges,並採取在該點的動作一個簡單的例子。

的例子表明,你可以修改數據前的最後提交,您還可以跟蹤並採取特別行動時,特定成員被更新:

namespace ExampleProject1.Models 
{ 
    public partial class ExampleProject1DataContext 
    { 
     public override void SubmitChanges(ConflictMode failureMode) 
     { 
      ManangeAndProcessChangeSet(base.GetChangeSet()); 
      base.SubmitChanges(failureMode); 
     } 

     private void ManangeAndProcessChangeSet(ChangeSet changeSet) 
     { 
      DateTime now = DateTime.UtcNow; 

      if (changeSet.Inserts.Count > 0) 
      { 
       ProcessInserts(changeSet.Inserts, now); 
      } 

      if (changeSet.Updates.Count > 0) 
      { 
       ProcessUpdates(changeSet.Updates, now); 
      } 

      if (changeSet.Deletes.Count > 0) 
      { 
       ProcessDeletes(changeSet.Deletes, now); 
      } 
     } 

     private void ProcessInserts(IList<object> list, DateTime now) 
     { 
      IEnumerable<Cake> cakes = list.OfType<Cake>(); 
      IEnumerable<Topping> toppings = list.OfType<Topping>(); 

      // Update the created and modified times. 
      foreach (Cake cake in cakes) 
      { 
       cake.Guid = Guid.NewGuid(); 
       cake.CreatedDateTime = now; 
       cake.ModifiedDateTime = now; 
      } 
      foreach (Topping topping in toppings) 
      { 
       topping.Guid = Guid.NewGuid(); 
       topping.CreatedDateTime = now; 
       topping.ModifiedDateTime = now; 
      } 
     } 

     private void ProcessUpdates(IList<object> list, DateTime now) 
     { 
      IEnumerable<Cake> cakes = list.OfType<Cake>(); 
      IEnumerable<Topping> toppings = list.OfType<Topping>(); 

      // Update the created and modified times. 
      foreach (Cake cake in cakes) 
      { 
       ProcessUpdates(cake, now); 
      } 
      foreach (Topping topping in toppings) 
      { 
       topping.ModifiedDateTime = now; 
      } 
     } 

     private void ProcessDeletes(IList<object> list, DateTime now) 
     { 
      IEnumerable<Cake> cakes = list.OfType<Cake>(); 
      IEnumerable<Topping> toppings = list.OfType<Topping>(); 

      // Could create tasks here to delete associated stored files for cakes and toppings. 
     } 

     private bool ProcessUpdates(Cake cake, DateTime now) 
     { 
      bool modified = false; 

      ModifiedMemberInfo[] mmi = context.Cakes.GetModifiedMembers(cake); 
      foreach (ModifiedMemberInfo mi in mmi) 
      { 
       switch (mi.Member.Name) 
       { 
        case "CountOfItemsSold": 
         // Exclude from updating the modified date. 
         break; 

        case "IsExpired": 
         if ((bool)mi.CurrentValue) 
         { 
          cake.ExpiredDateTime = now; 
         } 
         else 
         { 
          cake.ExpiredDateTime = null; 
         } 
         modified = true; 
         break; 

        default: 
         modified = true; 
         break; 
       } 
      } 

      if (modified) 
      { 
       cake.ModifiedDateTime = now; 
      } 

      return modified; 
     } 

    } 
} 
+0

爲什麼蛋糕和澆頭?那麼,爲什麼不呢? :-) – 2010-11-23 01:02:01

0

目前還不清楚您所使用的編輯機制,但如果多個DataContext都參與其中,是的:你必須告訴刪除的提交數據上下文(通過DeleteOnSubmit)。當然,它需要知道更改 - 否則它不會知道更改數據庫。

+0

我使用多個DataContext的,所以我需要告訴datacontext的變化是不理想的。我寧願如果LINQ照顧這個對我來說,但不幸它dosent.thanks你的幫助 – Somedeveloper 2010-11-20 02:25:24

1

如果你有一個N層應用程序,這是不可能的,因爲你將以斷開的方式進行更新,刪除和插入。

換句話說,將使用一個數據上下文來創建列表,實體將通過WCF或其他機制傳遞給客戶端,然後發送回DAL,另一個數據上下文將用於做更新。