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