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
When creating the AppDbContext and inheriting from DbContext, you can pass the default options to DbContext using DI like this:
internal class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
{
}
}
Or you can just pass nothing like this:
internal class AppDbContext : DbContext
{
public AppDbContext() : base()
{
}
}
In both cases you have the context registered like this, without options:
services..AddDbContext<AppDbContext>()
This has some differences though. In the case where you don't pass the options, logging does not work. This is because when EFCore creates the ScopedLoggerFactory it needs the application service provider to have the application ILoggerFactory, which you only have when injecting the options via DI. So what it does is creating a new LoggerFactory() which does not log anything.
return new ScopedLoggerFactory(new LoggerFactory(), dispose: true);
I think we expect both cases to have the same behavior. One solution can be removing the parameterless DbContext constructor, but I don't know if that's feasible.
The text was updated successfully, but these errors were encountered:
@michaeltg17 EF is intended to be used from a D.I. container, in which case services from D.I. are passed to EF via the DbContextOptions parameter on the DbContext constructor. EF is also intended to be used without D.I., in which case the parameterless constructor can be used. In this case, there is no place to obtain D.I. services, such as a logging provider, and so these services are not available. They can be registered manually, if desired.
So, there is no way for these two constructors to perform the same way, and indeed, it is by-design that they do not.
When creating the AppDbContext and inheriting from DbContext, you can pass the default options to DbContext using DI like this:
Or you can just pass nothing like this:
In both cases you have the context registered like this, without options:
This has some differences though. In the case where you don't pass the options, logging does not work. This is because when EFCore creates the ScopedLoggerFactory it needs the application service provider to have the application ILoggerFactory, which you only have when injecting the options via DI. So what it does is creating a new LoggerFactory() which does not log anything.
I think we expect both cases to have the same behavior. One solution can be removing the parameterless DbContext constructor, but I don't know if that's feasible.
The text was updated successfully, but these errors were encountered: