簡體   English   中英

使用DBSet本地進行多個查詢

[英]using the DBSet local for multiple queries

我正在嘗試使用本地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);

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

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

每個查詢使用上下文。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM