diff --git a/src/EFCore/Internal/DbContextFactorySource.cs b/src/EFCore/Internal/DbContextFactorySource.cs index 66f126e2fe5..0689fda135d 100644 --- a/src/EFCore/Internal/DbContextFactorySource.cs +++ b/src/EFCore/Internal/DbContextFactorySource.cs @@ -39,7 +39,7 @@ public DbContextFactorySource() { var constructors = typeof(TContext).GetTypeInfo().DeclaredConstructors - .Where(c => !c.IsStatic && c.IsPublic) + .Where(c => !c.IsStatic && c.IsPublic && c.GetParameters().Length != 0) .ToArray(); if (constructors.Length == 1) diff --git a/test/EFCore.Tests/DbContextFactoryTest.cs b/test/EFCore.Tests/DbContextFactoryTest.cs index 1e83b8e2ba3..2fed2d03833 100644 --- a/test/EFCore.Tests/DbContextFactoryTest.cs +++ b/test/EFCore.Tests/DbContextFactoryTest.cs @@ -18,10 +18,21 @@ public class DbContextFactoryTest [InlineData(ServiceLifetime.Scoped)] [InlineData(ServiceLifetime.Transient)] public void Factory_creates_new_context_instance(ServiceLifetime lifetime) + => ContextFactoryTest(lifetime); + + [ConditionalTheory] + [InlineData(ServiceLifetime.Singleton)] + [InlineData(ServiceLifetime.Scoped)] + [InlineData(ServiceLifetime.Transient)] + public void Factory_creates_new_context_instance_with_additional_parameterless_constructor(ServiceLifetime lifetime) + => ContextFactoryTest(lifetime); + + private static void ContextFactoryTest(ServiceLifetime lifetime) + where TContext : DbContext { var serviceProvider = (IServiceProvider)new ServiceCollection() - .AddDbContextFactory( - b => b.UseInMemoryDatabase(nameof(WoolacombeContext)), + .AddDbContextFactory( + b => b.UseInMemoryDatabase(nameof(TContext)), lifetime) .BuildServiceProvider(validateScopes: true); @@ -30,14 +41,14 @@ public void Factory_creates_new_context_instance(ServiceLifetime lifetime) serviceProvider = serviceProvider.CreateScope().ServiceProvider; } - var contextFactory = serviceProvider.GetService>(); + var contextFactory = serviceProvider.GetService>(); using var context1 = contextFactory.CreateDbContext(); using var context2 = contextFactory.CreateDbContext(); Assert.NotSame(context1, context2); - Assert.Equal(nameof(WoolacombeContext), GetStoreName(context1)); - Assert.Equal(nameof(WoolacombeContext), GetStoreName(context2)); + Assert.Equal(nameof(TContext), GetStoreName(context1)); + Assert.Equal(nameof(TContext), GetStoreName(context2)); } [ConditionalFact] @@ -164,6 +175,18 @@ public void Lifetime_is_singleton_when_pooling() serviceCollection.Single(e => e.ServiceType == typeof(DbContextOptions)).Lifetime); } + private class GruntaContext : DbContext + { + public GruntaContext() + { + } + + public GruntaContext(DbContextOptions options) + : base(options) + { + } + } + private class WoolacombeContext : DbContext { public WoolacombeContext(DbContextOptions options) @@ -187,6 +210,10 @@ public void Factory_can_use_constructor_with_non_generic_builder() private class CroydeContext : DbContext { + public CroydeContext() + { + } + public CroydeContext(DbContextOptions options) : base(options) {