1

私は EF 6 を使用しており、System.Data.Entity.Infrastructure.Interception 名前空間から IDbCommandInterceptor を使用しようとしています。

読み取りと更新にはうまく機能しますが、新しいエンティティをデータベースに追加すると、NonQueryExecuted() も NonQueryExecuting() も起動しません。これは Interceptor による通常の動作ですか、それとも私の側で何かが正しく実装されていませんか?

コード:

インターセプター:

    public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
            this.MonitorAndNotifySignalRService(command);
        }

public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
    }

    public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
        }

        public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
        }

    public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
        }

    public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
        }

データコンテキストベース:

        static Constructor()
    {
        Database.SetInitializer<TDataContext>(null);        // turn off database initialization so the db schema is not changed from the model
        DbInterception.Add(new Interceptor());
    }
4

1 に答える 1

4

これは仕様によるものです。SELECTデータベースで生成された ID を持つエンティティが挿入されると、insert ステートメントに、生成された ID 値を挿入されたエンティティの Id プロパティに読み込むステートメントが付随します。コマンドの形状は次のとおりです。

INSERT [dbo].[Table](...)
VALUES (...)
SELECT [Id]
FROM [dbo].[Table]
WHERE @@ROWCOUNT > 0 AND [Id] = scope_identity()

ReaderExecuting/ReaderExecutedペアをトリガーします。

一方、エンティティがデータベースで生成された ID を持っていない場合、エンティティは後続のSELECTステートメントなしで挿入できます。この場合、NonQueryExecuting/NonQueryExecutedペアがトリガーされます。(私が自分自身をテストしたように)。

于 2014-03-19T23:17:44.293 に答える