You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I've been using npsql with EF core for a while now, while running locally (non prod environemnt), we get spammed with
Microsoft.EntityFrameworkCore.Infrastructure[10403]
Entity Framework Core 6.0.2 initialized 'MyContext' using provider 'Npgsql.EntityFrameworkCore.PostgreSQL:6.0.3+94d0a8eaeb9cf5af6ce55a9e59153c1a1be3244f' with options: SensitiveDataLoggingEnabled using snake-case naming (culture=)
on all code paths that accesses the db context. Having trouble understanding/deep diving into this issue as I don't know why the context keeps getting initialized every single time we access it. Is this expected behavior? this is how we are registering our context,
what steps can we take to understand what's causing this? Could this be happening because we're registering the context with AddScoped? Or is this just expected behavior?
services.AddDbContext<MyContext>(x =>ConfigureDbContext(x,awsRegion,isDevelopment,loggerFactory));services.AddScoped<IUnitOfWork,UnitOfWork>();services.AddScoped<IMyContext>(provider =>provider.GetService<MyContext>());privatestaticvoidConfigureDbContext(DbContextOptionsBuilderoptions,RegionEndpointawsRegion,boolisDevelopment,ILoggerFactoryloggerFactory){stringconnectionString=RDSConnectionStringFactory.GetRDSConnectionString(awsRegion);
#if DEBUG||TESToptions.UseLoggerFactory(loggerFactory);// entity framework logging remove when not debugging. EF logging ef core logging.
#endif
try{options.UseNpgsql(connectionString, x =>{x.EnableRetryOnFailure(5,TimeSpan.FromSeconds(10),newList<string>());}).UseSnakeCaseNamingConvention();if(isDevelopment){options.EnableSensitiveDataLogging();}}catch(Exceptionexception){Console.Write(exception.Message);}}
This is the expected behavior; AddDbContext creates a new context for each web request (that's what Scoped means), and each new context that gets initialized makes EF log that message. This isn't indicative of a problem or anything, though I've opened dotnet/efcore#28480 to consider downgrading that message to be Debug-level rather than Information.
Note that you can use context pooling to recycle context instances, which may improve perf a bit and would make most of those messages go away (once a sufficient number of contexts is initialized and pooled).
I'm going to go ahead and close this as by-design, but feel free to post back here.
This does the trick in .net6:
.MinimumLevel.Override("Microsoft.EntityFrameworkCore.Database.Command", LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.EntityFrameworkCore", LogEventLevel.Warning) // this is the one that hides it
Hi, I've been using npsql with EF core for a while now, while running locally (non prod environemnt), we get spammed with
on all code paths that accesses the db context. Having trouble understanding/deep diving into this issue as I don't know why the context keeps getting initialized every single time we access it. Is this expected behavior? this is how we are registering our context,
what steps can we take to understand what's causing this? Could this be happening because we're registering the context with AddScoped? Or is this just expected behavior?
Further technical details
Npgsql version: 6.0.3
PostgreSQL version: 12.4
Operating system: Ubuntu 22
The text was updated successfully, but these errors were encountered: