2013-01-17 35 views
0

我正在嘗試使用本地DBSet運行查詢,但遇到了我提出的兩個解決方案的問題。使用DBSet本地進行多個查詢

解決方案1:這將運行查詢並將查詢結果綁定到我的結果控件,並且數據可以被編輯並保存到數據庫。但是,如果我嘗試在同一個表上運行另一個查詢(如果我切換表一切正常),結果項不會更新(它將添加任何新結果,但任何舊結果應該不再存在)

  //This part gets a DBSet from the context with the passed in table 
      Type t = context.GetType(); 
      dynamic myDBSet= t.InvokeMember(table, 
         BindingFlags.DeclaredOnly | 
         BindingFlags.Public | BindingFlags.NonPublic | 
         BindingFlags.Instance | BindingFlags.GetProperty, null, context, new object[0]); 

      //This is the query I want to run. I am then loading it into the localDBSet 
      ((IQueryable)myDBSet).AsQueryable().Where(condition).Load(); 
      results.ItemsSource = myDBSet.Local; 

解決方案2:這將運行查詢並正確綁定數據。另外,數據將更新所有新的查詢,但在DataGrid中的數據不再是可編輯

  //This part gets a DBSet from the context with the passed in table 
      Type t = context.GetType(); 
      dynamic myDBSet= t.InvokeMember(table, 
         BindingFlags.DeclaredOnly | 
         BindingFlags.Public | BindingFlags.NonPublic | 
         BindingFlags.Instance | BindingFlags.GetProperty, null, context, new object[0]); 

      //This is the query I want to run. I am then loading it into the localDBSet 
      ((IQueryable)myDBSet).AsQueryable().Where(condition).Load(); 
      DbSet copiedDBSet= myDBSet; 
      results.ItemsSource = copiedDBSet.Local.AsQueryable().Where(condition); 

有沒有人有一個解決方案,可以讓我運行多個查詢,仍然編輯數據?

回答

0

有沒有人有一個解決方案,將允許我運行多個查詢 仍然編輯數據?

對每個查詢使用上下文。

這樣你可以使用解決方案1並直接綁定到本地可觀察集合。如果你不想要以前的查詢結果,那麼沒有理由保持上下文。任何你動態調用上下文dbset方法的原因,而不是使用通用的context.Set<T>()