【发布时间】:2021-07-09 10:22:23
【问题描述】:
我正在使用 EF 命令拦截器在命令执行后将一些数据保存到数据库。 (我有一些过滤器只在我想要的命令上运行它,所以它不会变成无限循环)
class EFCommandInterceptor : IDbCommandInterceptor
{
public void ReaderExecuted(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
var log = new LogModel
{
DateTime = DateTime.Now,
Log = "Log"
};
using (var context = new ApplicationDbContext())
{
context.Logs.Add(log);
var res = context.SaveChangesAsync().Result;
}
}
}
这在大多数情况下都可以正常工作。但是,如果我有任何需要在事务中记录的命令
using (TransactionScope transaction = new TransactionScope())
{
// Some commands to intercept
}
我收到一个错误
var res = context.SaveChangesAsync().Result;
Win32Exception: The wait operation timed out
我尝试过延长连接超时和命令超时计时器,但引发相同错误需要更长的时间。
我也尝试在拦截器中创建一个新的 sql 连接和一个命令,但得到了同样的错误。
当 EF 命令拦截器从事务内部拦截命令时,有没有办法将数据保存到数据库中?
如果没有,拦截器是否有办法忽略事务内部的命令?
【问题讨论】:
标签: entity-framework transactions interceptor