変更を行った実際のユーザーとともに、すべてのデータ変更をロギング テーブルに記録する必要があります。アプリケーションは 1 人の SQL ユーザーを使用してデータベースにアクセスしていますが、「実際の」ユーザー ID をログに記録する必要があります。
テーブルの挿入と更新ごとにトリガーを記述し、context_info を使用してユーザー ID を保存することで、t-sql でこれを行うことができます。ユーザー ID をストアド プロシージャに渡し、ユーザー ID を contextinfo に格納すると、トリガーはこの情報を使用してログ行をログ テーブルに書き込むことができます。
EFを使用して同様のことをどこで、またはどのように行うことができるか、場所または方法が見つかりません。したがって、主な目標は次のとおりです。EFを介してデータを変更した場合、正確なデータ変更を半自動でテーブルに記録したいと思います(そのため、変更前にすべてのフィールドをチェックしたくありませんオブジェクトを保存します)。EntitySQL を使用しています。
残念ながら、SQL 2000 に固執する必要があるため、SQL2008 で導入されたデータ変更キャプチャはオプションではありません (しかし、それも適切な方法ではない可能性があります)。
アイデア、リンク、出発点はありますか?
[編集] いくつかのメモ: ObjectContext.SavingChanges イベントハンドラーを使用することで、SQL ステートメントを挿入して contextinfo を初期化できるポイントを取得できます。ただし、EF と標準 SQL を混在させることはできません。したがって、EntityConnection を取得できますが、それを使用して T-SQL ステートメントを実行することはできません。または、EntityConnection の接続文字列を取得し、それに基づいて SqlConnection を作成することもできますが、それは別の接続になるため、contextinfo は EF による保存に影響しません。
SavingChanges ハンドラーで次のことを試しました。
testEntities te = (testEntities)sender;
DbConnection dc = te.Connection;
DbCommand dcc = dc.CreateCommand();
dcc.CommandType = CommandType.StoredProcedure;
DbParameter dp = new EntityParameter();
dp.ParameterName = "userid";
dp.Value = textBox1.Text;
dcc.CommandText = "userinit";
dcc.Parameters.Add(dp);
dcc.ExecuteNonQuery();
エラー: EntityCommand.CommandText の値は StoredProcedure コマンドに対して有効ではありません。EntityParameter の代わりに SqlParameter を使用した場合も同様です。SqlParameter は使用できません。
StringBuilder cStr = new StringBuilder("declare @tx char(50); set @tx='");
cStr.Append(textBox1.Text);
cStr.Append("'; declare @m binary(128); set @m = cast(@tx as binary(128)); set context_info @m;");
testEntities te = (testEntities)sender;
DbConnection dc = te.Connection;
DbCommand dcc = dc.CreateCommand();
dcc.CommandType = CommandType.Text;
dcc.CommandText = cStr.ToString();
dcc.ExecuteNonQuery();
エラー: クエリ構文が無効です。
ここで、Entity Framework と ADO.NET の間のブリッジを作成することに行き詰まっています。動作させることができれば、概念実証を投稿します。