紀錄 EF 所生成的 SQL 語法
常想把 LinqToSql 印出來以利 Debug 調校效能
以ABP來說是位於公共設施層 - EntityFrameworkCore 專案
注入 ILoggerFactory 然後 UseLoggerFactory
EntityFrameworkModule.cs
private readonly ILoggerFactory _loggerFactory;
public OBManEntityFrameworkModule(ILoggerFactory loggerFactory)
{
_loggerFactory = loggerFactory;
}
public override void PreInitialize()
{
Configuration.Modules.AbpEfCore().AddDbContext<FirstDbContext>(options=>
{
if (options.ExistingConnection != null)
FirstDbContextOptionsConfigurer
.Configure(options.DbContextOptions, options.ExistingConnection);
else
FirstDbContextOptionsConfigurer
.Configure(options.DbContextOptions, options.ConnectionString);
// add sql log
options.DbContextOptions.UseLoggerFactory(loggerFactory)
.EnableSensitiveDataLogging();
});
}
執行有跑到SQL就會印出語法到主控台