efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Document that IDbContextFactory<TContext> is not covariant

Open Driedas opened this issue 3 years ago • 3 comments

According to the official documentation of IDbContextFactory<TContext>, 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>>();

Driedas avatar Jan 19 '23 09:01 Driedas

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

roji avatar Jan 19 '23 10:01 roji

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...

Driedas avatar Jan 19 '23 11:01 Driedas

Re-opening to fix docs.

ajcvickers avatar Jan 19 '23 11:01 ajcvickers