0

ローカル DBSet を使用してクエリを実行しようとしていますが、思いついた 2 つの解決策に問題があります。

解決策 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: これにより、クエリが実行され、データが正しくバインドされます。また、データはすべての新しいクエリに対して更新されますが、データグリッド内のデータは編集できなくなります

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

複数のクエリを実行してデータを編集できるソリューションを誰かが持っていますか?

4

1 に答える 1

0

複数のクエリを実行してデータを編集できるソリューションを持っている人はいますか?

クエリごとにコンテキストを使用します。

そうすれば、ソリューション 1 を使用して、ローカルの監視可能なコレクションに直接バインドできます。以前のクエリ結果が必要ない場合は、コンテキストを保持する理由はありません。コンテキスト dbset メソッドを動的に呼び出していて、ジェネリックを使用していない理由はありますcontext.Set<T>()か?

于 2013-01-17T20:20:00.867 に答える