c# - 在 EntityFramework6 中注册 SQLServer“消息”
问题描述
是否可以从 SQLServer 注册消息?Microsoft SQL Server Management Studio 的“消息”选项卡中显示的信息。
我发现了context.Database.Log
,但它没有按预期工作。SSMS 上显示的内容:
(3 rows affected)
Completion time: 2020-05-15T15:12:34.3776215+02:00
C# 端出现的内容:
Closed connection at 15.05.2020 15:36:02 +02:00
这不是完全相同的操作,但无论哪种方式,EF6 都只显示关闭的连接,而 SSMS 显示受影响的行和最终错误。
编辑
还有示例代码片段:
context.Database.Log = SQLServerLogHandler.LogDisplay.ServerLogDisplay;
context.Configuration.LazyLoadingEnabled = false;
context.Employees.Add(record);
await context.SaveChangesAsync();
System.Diagnostics.Debug.WriteLine("Message: " + SQLServerLogHandler.LogDisplay.Log + "- End of message");
public static class LogDisplay
{
public static string Log { get; set; }
public static void ServerLogDisplay(string log)
{
Log = log;
}
}