Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架检查所有查询?_C#_Entity Framework_Exception Handling_Database Connection_Crud - Fatal编程技术网

C# 实体框架检查所有查询?

C# 实体框架检查所有查询?,c#,entity-framework,exception-handling,database-connection,crud,C#,Entity Framework,Exception Handling,Database Connection,Crud,我正在重构我的项目异常。本项目采用实体框架。我想在对数据库执行所有操作之前检查数据库连接 我认为,我应该在数据访问层编写一个重写方法,该方法在所有查询之前运行。此方法将检查连接,如果存在连接问题,则应抛出DBConnectionException->(派生类) 如何在DAL或上下文类上执行此操作?可能会有所帮助,对于EF-6,您可以尝试使用DbCommandInterceptor public class DbConnectionCheckInterceptor : DbCommandInter

我正在重构我的项目异常。本项目采用实体框架。我想在对数据库执行所有操作之前检查数据库连接

我认为,我应该在数据访问层编写一个重写方法,该方法在所有查询之前运行。此方法将检查连接,如果存在连接问题,则应抛出
DBConnectionException
->(派生类)


如何在DAL或上下文类上执行此操作?

可能会有所帮助,对于EF-6,您可以尝试使用DbCommandInterceptor

public class DbConnectionCheckInterceptor : DbCommandInterceptor
    {
        public override void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
        {
             //check connection
        }

        public override void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
        {
             //check connection
        }

        public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
        {
             //check connection
        }
    }

谢谢,但该项目包括实体框架5.0
DbInterception.Add(new DbConnectionCheckInterceptor());