【发布时间】:2021-04-15 15:32:05
【问题描述】:
我需要拦截特定上下文的所有查询,但我的拦截器会捕获来自其他上下文的查询,是否可以只为特定上下文注册它?
我只需要修改到 Oracle 数据库的查询,但是这个解决方案从与 mssql 数据库相关的上下文中捕获查询。 我还用 Autofac 注册了我的上下文。
有没有更好的方法在执行之前修改 EF6 生成的查询?
class OracleDbCommandInterceptor : IDbCommandInterceptor
{
public void NonQueryExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void NonQueryExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}
public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
}
public void ReaderExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
}
public void ScalarExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
public void ScalarExecuting(System.Data.Common.DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
}
这就是我的上下文的样子
public abstract class OracleDbContext : oracleDb
{
static OracleDbContext()
{
DbInterception.Add(new OracleDbCommandInterceptor());
}
}
这是由 edmx 生成的
public partial class oracleDb: DbContext
{
public oracleDb()
: base("name=oracleDb")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Employees> Employees { get; set; }
}
【问题讨论】:
标签: c# .net oracle entity-framework-6 interceptor