如何防止EFcore5在保存时创建保存点
我们在始终在外部管理事务的上下文中使用 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))'.
我对禁用保存点很好(至少现在是这样),但我对每个数据库保存的日志消息不太满意......
有没有办法告诉 EF Core 不使用这个新功能?
回答
事实证明,ConfigureWarnings错误消息中描述的调用也可用于忽略消息。
然而要注意的一件事是,与警告消息所说的相反,该SavepointsDisabledBecauseOfMARS值不是RelationalEventId类型的一部分。相反,它是SqlServerEventId类型的一部分。
要忽略警告,完整代码如下:
ConfigureWarnings(w =>
w.Ignore(SqlServerEventId.SavepointsDisabledBecauseOfMARS))