Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle parameterless constructor in DbContextFactorySource #24717

Merged
merged 1 commit into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ private static Func<IServiceProvider, DbContextOptions<TContext>, TContext> Crea
{
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