gpt4 book ai didi

c# - 从 IDbCommandInterceptor 的实现中获取 DbContext

转载 作者:行者123 更新时间:2023-11-30 15:26:39 26 4
gpt4 key购买 nike

我正在使用 IDbCommandInterceptor 实现:

public class MyInterceptor : IDbCommandInterceptor
{
public void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
var context = interceptionContext.DbContexts.FirstOrDefault();
}

public void NonQueryExecuting(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}

public void ScalarExecuting(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}

public void NonQueryExecuted(DbCommand command, DbCommandInterceptionContext<int> interceptionContext)
{
}

public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
}

public void ScalarExecuted(DbCommand command, DbCommandInterceptionContext<object> interceptionContext)
{
}
}

由此注入(inject):

public class TestContext : System.Data.Entity.DbContext
{
// …

public TestContext()
: base("TestConnectionString")
{
Database.SetInitializer<TestContext>(null);
DbInterception.Add(new MyInterceptor());
}
}

(也在静态构造函数中尝试过)。

但是 interceptionContext.DbContexts 总是空的。如何获取执行上下文的实例?可能吗?

最佳答案

这并没有完全回答我的问题,但我在 Entity Framework documentation 中找到了解释是最准确的:

It’s worth noting that the interception context is a best effort to provide contextual information. However, in some corner cases some information that you would expect to be there may not be there. This is because EF has code paths that cannot easily be changed and do not include information that might be expected. For example, when EF makes a call into a provider, the provider has no knowledge of the DbContext being used. If that provider, outside of EF, decides to call ExecuteNonQuery, then two things might happen:

  • First the provider may just make the call directly, avoiding EF interception completely. (This is a consequence of having interception at the EF level rather than lower in the stack. It would be great if interception were lower in the stack, but this is unfortunately outside of the control of the EF team.)
  • If the provider is aware of EF interception then it can dispatch the ExecuteNonQuery call through EF interceptors. This means that any registered interceptor will be notified and can act appropriately. This is what the SQL Server and SQL Server Compact providers do. However, even when a provider does this it is likely that the DbContext being used will not be included in the interception context because the provider has no knowledge of it, and a change to allow this would break the well-defined provider APIs.

Luckily this kind of situation is rare and will likely not be an issue for most applications.

我不知道我的情况有多“罕见”,但也许我做错了什么……

关于c# - 从 IDbCommandInterceptor 的实现中获取 DbContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28995593/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com