efcore
efcore copied to clipboard
Inheriting DbContext with default options or without them should have the same behavior
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.