Refactor and enhance DbContext configuration

Updated `AddRecInfrastructure` to improve DbContext setup:
- Renamed parameters for clarity (`dbContextOpt` to `opt`).
- Renamed `connectionString` to `cnnStr` for consistency.
- Added `LogTo` for SQL query logging at `Trace` level.
- Enabled `EnableSensitiveDataLogging` for debugging.
- Enabled `EnableDetailedErrors` for detailed error messages.
This commit is contained in:
tekh 2025-12-03 11:48:23 +01:00
parent edf2468a33
commit a7f4677ad1

View File

@ -16,14 +16,14 @@ builder.Services.AddRecServices(options =>
builder.Services.AddRecInfrastructure(options => builder.Services.AddRecInfrastructure(options =>
{ {
options.ConfigureDbContext((provider, dbContextOpt) => options.ConfigureDbContext((provider, opt) =>
{ {
var connectionString = builder.Configuration.GetConnectionString("Default") var cnnStr = builder.Configuration.GetConnectionString("Default")
?? throw new InvalidOperationException("Connection string is not found."); ?? throw new InvalidOperationException("Connection string is not found.");
var logger = provider.GetRequiredService<ILogger<RecDbContext>>(); var logger = provider.GetRequiredService<ILogger<RecDbContext>>();
dbContextOpt.UseSqlServer(connectionString) opt.UseSqlServer(cnnStr)
.LogTo(log => logger.LogInformation("{log}", log), Microsoft.Extensions.Logging.LogLevel.Trace) .LogTo(log => logger.LogInformation("{log}", log), LogLevel.Trace)
.EnableSensitiveDataLogging() .EnableSensitiveDataLogging()
.EnableDetailedErrors(); .EnableDetailedErrors();
}); });