覆盖DbQueryProvider中的方法,以便从DbContext继承程序捕获异常

本文关键字:DbContext 继承 程序 捕获异常 DbQueryProvider 方法 覆盖 | 更新日期: 2023-09-27 18:15:55

首先在实体框架6中使用代码,我想记录连接到DbContext内服务器的失败。我可以覆盖SaveChanges并捕获任何错误。这部分效果很好。但是,我不知道在DbContext中哪里可以捕获在公开集合上运行的查询的异常。这些可以在DbContext内部捕获吗?这是我的电话。我可以重写这个堆栈中的任何方法吗?

EntityFramework.SqlServer.dll!System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute<System.Data.Entity.Core.Objects.ObjectResult<Asi.Server.History.DBOs.LogSourceDbo>>(System.Func<System.Data.Entity.Core.Objects.ObjectResult<Asi.Server.History.DBOs.LogSourceDbo>> operation) + 0xfd bytes  
    EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<Asi.Server.History.DBOs.LogSourceDbo>.GetResults(System.Data.Entity.Core.Objects.MergeOption? forMergeOption) + 0x286 bytes 
    EntityFramework.dll!System.Data.Entity.Core.Objects.ObjectQuery<Asi.Server.History.DBOs.LogSourceDbo>.System.Collections.Generic.IEnumerable<T>.GetEnumerator.AnonymousMethod__0() + 0x45 bytes 
    EntityFramework.dll!System.Data.Entity.Internal.LazyEnumerator<Asi.Server.History.DBOs.LogSourceDbo>.MoveNext() + 0x45 bytes    
    System.Core.dll!System.Linq.Enumerable.SingleOrDefault<Asi.Server.History.DBOs.LogSourceDbo>(System.Collections.Generic.IEnumerable<Asi.Server.History.DBOs.LogSourceDbo> source) + 0x139 bytes 
    EntityFramework.dll!System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.GetElementFunction.AnonymousMethod__2<Asi.Server.History.DBOs.LogSourceDbo>(System.Collections.Generic.IEnumerable<Asi.Server.History.DBOs.LogSourceDbo> sequence) + 0x54 bytes   
    EntityFramework.dll!System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle<Asi.Server.History.DBOs.LogSourceDbo>(System.Collections.Generic.IEnumerable<Asi.Server.History.DBOs.LogSourceDbo> query, System.Linq.Expressions.Expression queryRoot) + 0x6c bytes    
    EntityFramework.dll!System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute<Asi.Server.History.DBOs.LogSourceDbo>(System.Linq.Expressions.Expression expression) + 0xc6 bytes  
    EntityFramework.dll!System.Data.Entity.Internal.Linq.DbQueryProvider.Execute<Asi.Server.History.DBOs.LogSourceDbo>(System.Linq.Expressions.Expression expression) + 0x9c bytes  
    System.Core.dll!System.Linq.Queryable.SingleOrDefault<Asi.Server.History.DBOs.LogSourceDbo>(System.Linq.IQueryable<Asi.Server.History.DBOs.LogSourceDbo> source, System.Linq.Expressions.Expression<System.Func<Asi.Server.History.DBOs.LogSourceDbo,bool>> predicate) + 0x120 bytes    

覆盖DbQueryProvider中的方法,以便从DbContext继承程序捕获异常

我能够通过添加IDbConnectionInterceptor的实现并检查每个被覆盖的方法中的异常是否为null来完成此操作。