Skip to content

Commit

Permalink
Handle parameterless constructor in DbContextFactorySource
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Apr 21, 2021
1 parent acfec75 commit 7d34e99
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/Internal/DbContextFactorySource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 32 additions & 5 deletions test/EFCore.Tests/DbContextFactoryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,21 @@ public class DbContextFactoryTest
[InlineData(ServiceLifetime.Scoped)]
[InlineData(ServiceLifetime.Transient)]
public void Factory_creates_new_context_instance(ServiceLifetime lifetime)
=> ContextFactoryTest<WoolacombeContext>(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<GruntaContext>(lifetime);

private static void ContextFactoryTest<TContext>(ServiceLifetime lifetime)
where TContext : DbContext
{
var serviceProvider = (IServiceProvider)new ServiceCollection()
.AddDbContextFactory<WoolacombeContext>(
b => b.UseInMemoryDatabase(nameof(WoolacombeContext)),
.AddDbContextFactory<TContext>(
b => b.UseInMemoryDatabase(nameof(TContext)),
lifetime)
.BuildServiceProvider(validateScopes: true);

Expand All @@ -30,14 +41,14 @@ public void Factory_creates_new_context_instance(ServiceLifetime lifetime)
serviceProvider = serviceProvider.CreateScope().ServiceProvider;
}

var contextFactory = serviceProvider.GetService<IDbContextFactory<WoolacombeContext>>();
var contextFactory = serviceProvider.GetService<IDbContextFactory<TContext>>();

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]
Expand Down Expand Up @@ -164,6 +175,18 @@ public void Lifetime_is_singleton_when_pooling()
serviceCollection.Single(e => e.ServiceType == typeof(DbContextOptions<WoolacombeContext>)).Lifetime);
}

private class GruntaContext : DbContext
{
public GruntaContext()
{
}

public GruntaContext(DbContextOptions<GruntaContext> options)
: base(options)
{
}
}

private class WoolacombeContext : DbContext
{
public WoolacombeContext(DbContextOptions<WoolacombeContext> options)
Expand All @@ -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)
{
Expand Down

0 comments on commit 7d34e99

Please sign in to comment.