如何防止EF core 5在保存时创建保存点

Rob*_*bba 3 entity-framework-core-5

我们在始终在外部管理事务的上下文中使用 EF Core。我们还必须使用 MARS。由于我们已升级到 EF Core 5,此组合会导致以下警告:

Microsoft.EntityFrameworkCore.Database.Transaction - Savepoints are disabled because Multiple Active 
Result Sets (MARS) is enabled. If 'SaveChanges' fails, then the transaction cannot be automatically 
rolled back to a known clean state. Instead, the transaction should be rolled back by the application 
before retrying 'SaveChanges'. See https://go.microsoft.com/fwlink/?linkid=2149338 for more information. 
To identify the code which triggers this warning, call 'ConfigureWarnings(w => 
w.Throw(RelationalEventId.SavepointsDisabledBecauseOfMARS))'.
Run Code Online (Sandbox Code Playgroud)

我对禁用保存点很好(至少现在是这样),但我对每个数据库保存的日志消息不太满意......

有没有办法告诉 EF Core 不使用这个新功能?

Rob*_*bba 7

事实证明,ConfigureWarnings错误消息中描述的调用也可用于忽略消息。

然而要注意的一件事是,与警告消息所说的相反,该SavepointsDisabledBecauseOfMARS值不是RelationalEventId类型的一部分。相反,它是SqlServerEventId类型的一部分。

要忽略警告,完整代码如下:

ConfigureWarnings(w => 
w.Ignore(SqlServerEventId.SavepointsDisabledBecauseOfMARS))
Run Code Online (Sandbox Code Playgroud)


小智 5

除了罗巴的回答之外,因为你可能需要知道在哪里放置这个方法,就像我一样。

您需要OnConfiguring在您的DbContext班级中重写:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.ConfigureWarnings(w => w.Ignore(SqlServerEventId.SavepointsDisabledBecauseOfMARS));
}
Run Code Online (Sandbox Code Playgroud)