Entity Framework 6.0 Tutorials(5):Command Interception

Interception:

Here, you will learn how to intercept EF when it executes database commands.

EF 6 provides the ability to intercept the context using IDbCommandInterceptor before and after it performs the ExecuteNonQuery, ExecuteScalar, ExecuteReader operations to the database.

First, implement IDbCommandInterceptor as shown below:

class EFCommandInterceptor: IDbCommandInterceptor
{
    public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        LogInfo("NonQueryExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }

    public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
    {
        LogInfo("NonQueryExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync,  command.CommandText));
    }

    public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContextt<System.Data.Common.DbDataReader> interceptionContext)
    {
        LogInfo("ReaderExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }

    public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
    {
        LogInfo("ReaderExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }

    public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        LogInfo("ScalarExecuted", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }

    public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
    {
        LogInfo("ScalarExecuting", String.Format(" IsAsync: {0}, Command Text: {1}", interceptionContext.IsAsync, command.CommandText));
    }

    private void LogInfo(string command, string commandText)
    {
        Console.WriteLine("Intercepted on: {0} :- {1} ", command, commandText);
    }
}

You can see in the above code that IDbCommandInterceptor provides six methods to execute.

Now, you will need to configure the interceptor either by using config file or code-based configuration.

Config file:

<entityFramework>
    <interceptors>
        <interceptor type="EF6DBFirstTutorials.EFCommandInterceptor, EF6DBFirstTutorials">
        </interceptor>
    </interceptors>
</entityFramework>

Code-based config:

public class FE6CodeConfig : DbConfiguration
{
        public FE6CodeConfig()
        {
            this.AddInterceptor(new EFCommandInterceptor());
        }
}

So now, we can log commands whenever DbContext executes ExecuteNonQuery, ExecuteScalar, and ExecuteReader.

Download DB First sample project for interception demo.

时间: 2024-08-14 18:31:29

Entity Framework 6.0 Tutorials(5):Command Interception的相关文章

Entity Framework 6.0 Tutorials(1):Introduction

以下系统文章为EF6.0知识的介绍,本章是第一篇 原文地址:http://www.entityframeworktutorial.net/entityframework6/introduction.aspx ------------------------------------------------------------------------------------------------------------- Entity Framework 6.0 Introduction: W

Entity Framework 6.0 Tutorials(4):Database Command Logging

Database Command Logging: In this section, you will learn how to log commands & queries sent to the database by Entity Framework. Prior to EF 6, we used the database tracing tool or third party tracing utility to trace database queries and commands s

Entity Framework 6.0 Tutorials(2):Async query and Save

Async query and Save: You can take advantage of asynchronous execution of .Net 4.5 with Entity Framework. EF 6 has the ability to execute a query and command asynchronously using DbContext. Let's see how to execute asynchronous query first and then w

Entity Framework 6.0 Tutorials(6):Transaction support

Transaction support: Entity Framework by default wraps Insert, Update or Delete operation in a transaction, whenever you execute SaveChanges(). EF starts a new transaction for each operation and completes the transaction when the operation finishes.

Entity Framework 6.0 Tutorials(10):Index Attribute

Index Attribute: Entity Framework 6 provides Index attribute to create Index on a particular column in the database as shown below: class Student { public Student() { } public int Student_ID { get; set; } public string StudentName { get; set; } [Inde

Entity Framework 6.0 Tutorials(3):Code-based Configuration

Code-based Configuration: Entity Framework 6 has introduced code based configuration. Now, you can configure Entity Framework related settings using the code which had been previously configured in the <entityframework> section of the app.config. Ho

Entity Framework 6.0 Tutorials(9):Stored Procedure Mapping

Code First - Insert, Update, Delete Stored Procedure Mapping: Entity Framework 6 Code-First provides the ability to create and use a stored procedure for add, update, and delete operations. This was not possible in the previous versions of Entity Fra

Entity Framework 6.0 Tutorials(11):Download Sample Project

Download Sample Project: Download a sample project for Entity Framework 6 Database-First model below. Download a sample project for Entity Framework 6 CodeFirst-First below.  These sample projects already include the SchoolDB.mdf file required for th

Entity Framework 6.0 Tutorials(8):Custom Code-First Conventions

Custom Code-First Conventions: Code-First has a set of default behaviors for the models that are referred to as conventions. EF 6 provides the ability to define your own custom conventions which will be the default behavior for your models. There are