Subclassing an Existing DbContext for an Alternate Provider Causes Compilation Errors
In Entity Framework Core 3.1, when subclassing an existing DbContext, it will generate an error if you're missing a constructor:
public MySqliteContext(DbContextOptions<MySqliteContext> options) : base(options)
However, this will type error, because the constructor of the base class is expecting type DbContextOptions<MyContext> for options, not DbContextOptions<MySqliteContext>.
This is discussed an in this issue, and the work around is to add a protected constructor in the base class. https://github.com/dotnet/efcore/issues/7533#issuecomment-353669263
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 7b011c7a-3534-78e3-b491-addd96fb5ba1
- Version Independent ID: 371826c5-3ef2-4eef-32e9-d55c777cd9d3
- Content: Migrations with Multiple Providers - EF Core
- Content Source: entity-framework/core/managing-schemas/migrations/providers.md
- Product: entity-framework
- Technology: entity-framework-core
- GitHub Login: @bricelam
- Microsoft Alias: bricelam
As a small addendum to this, the once you've made the changes, you still need some code that will register the alternate class to the DI system, otherwise the migration will fail. For an ASP application, something like this would work:
if (_env.IsDevelopment())
{
services.AddDbContext<MyContext, MySqliteContext>(options =>
options.UseSqlite(Configuration.GetConnectionString("SqliteConnectionString"))
);
}
Though you'd also need to set $env:ASPNETCORE_ENVIRONMENT='Development' before running the migration command.