Allow a second type argument AddDbContext to support inheritance #10451
Labels
closed-fixed
The issue has been fixed and is/will be included in the release indicated by the issue milestone.
type-enhancement
Milestone
I have a project running in different environments with different databases (PostgreSQL / SQL Server). I tried using the same DbContext for both databases but had some problems with migrations since vendor specific code is added (e.g. data types).
I decided to make two distinct contexts, both extending my dbContext.
From now on I added migrations using
Add-Migration -Context PgSqlParcelLogisticsContext ChangedAwesomeFields
andAdd-Migration -Context SqlServerParcelLogisticsContext ChangedAwesomeFields
resulting in different Migrations with vendor specific code in them.The problem i was facing was with dependency injection. When adding EF like below I was unable to use my abstract base class for dependency injection.
Now exceptions are thrown when trying to get a context like so because Dependency Injection could not find a service for type
ParcelLogisticsDbContext
:To resolve this issue I added extension methods so I would be able to use
AddDbContext
with two type arguments in a similar way e.g.AddScoped
works and I could use it like this:Do you think this is a good solution or do you have any other ideas? I also submitted a Pull Request (#10454) and I am happy for feedback!
Further technical details
EF Core version: 2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer, Npgsql.EntityFrameworkCore.PostgreSQL
Operating system: Windows 10 Version 1703
IDE: Visual Studio 2017 15.4.5
The text was updated successfully, but these errors were encountered: