实体框架检查所有查询
本文关键字:查询 检查 框架 实体 | 更新日期: 2023-09-27 18:28:56
我正在重构我的项目异常。此项目中使用了实体框架。在对数据库执行所有操作之前,我想检查数据库连接。
我认为,我应该在数据访问层中编写一个重写的方法,它在所有查询之前运行。此方法将检查连接,如果存在连接问题,则应抛出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
}
}
启动应用程序之前
DbInterception.Add(new DbConnectionCheckInterceptor());