dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

Document that IDbContextFactory<TContext> is not covariant #30096

Open Driedas opened 1 year ago

Driedas commented 1 year ago

According to the official documentation of IDbContextFactory, the TContext parameter should be covariant:

TContext The DbContext type to create. This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.

However, that is not the case in the source code

public interface IDbContextFactory<[DynamicallyAccessedMembers(DbContext.DynamicallyAccessedMemberTypes)] TContext>
    where TContext : DbContext
{

Is this just a case of a copy paste issue from the documentation of Entity Framework, where the interface actually was covariant? Is there a reason why this is not supported in EF core? To me it looks like a textbook example of where covariance should work and enable to dynamically provide derived DbContext implementations with different configurations. For example we are using a derived DbContext in automated tests with certain entities configured slightly differently (e. g. sql server variant columns)

TLDR: The below code should work according to the documentation, and did work in old EF, but does not in EF core

IDbContextFactory<DbContextBase> contextFactory = _serviceProvider.GetRequiredService<IDbContextFactory<DbContextDerived>>();
roji commented 1 year ago

The question was raised #26630; the interface used to be covariant, but adding the async method removed that. We should fix the docs.

Driedas commented 1 year ago

right, thanks for the quick response, I did try looking for existing issues, but probably only open issues by mistake 👍

In the mean time I've implemented a workaround, coincidentally the same approach used in the linked issue, so we will stick with that.

Thanks...

ajcvickers commented 1 year ago

Re-opening to fix docs.