Skip to content

Commit

Permalink
Discover IDBContextFactory at design-time
Browse files Browse the repository at this point in the history
Fixes #21257
  • Loading branch information
ajcvickers committed Jun 14, 2020
1 parent 4fa6721 commit 343a23a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ where i.IsGenericType
contexts.Add(
context,
FindContextFactory(context)
?? FindContextFromRuntimeDbContextFactory(appServices, context)
?? (() => (DbContext)ActivatorUtilities.GetServiceOrCreateInstance(appServices, context)));
}

Expand All @@ -188,13 +189,15 @@ where i.IsGenericType
var types = _startupAssembly.GetConstructibleTypes()
.Concat(_assembly.GetConstructibleTypes())
.ToList();

var contextTypes = types.Where(t => typeof(DbContext).IsAssignableFrom(t)).Select(
t => t.AsType())
.Concat(
types.Where(t => typeof(Migration).IsAssignableFrom(t))
.Select(t => t.GetCustomAttribute<DbContextAttribute>()?.ContextType)
.Where(t => t != null))
.Distinct();

foreach (var context in contextTypes.Where(c => !contexts.ContainsKey(c)))
{
_reporter.WriteVerbose(DesignStrings.FoundDbContext(context.ShortDisplayName()));
Expand Down Expand Up @@ -254,6 +257,17 @@ public virtual ContextInfo GetContextInfo([CanBeNull] string contextType)
return info;
}

private Func<DbContext> FindContextFromRuntimeDbContextFactory(IServiceProvider appServices, Type contextType)
{
var factoryInterface = typeof(IDbContextFactory<>).MakeGenericType(contextType);
var service = appServices.GetService(factoryInterface);
return service == null
? (Func<DbContext>)null
: () => (DbContext)factoryInterface
.GetMethod(nameof(IDbContextFactory<DbContext>.CreateDbContext))
?.Invoke(service, null);
}

private Func<DbContext> FindContextFactory(Type contextType)
{
var factoryInterface = typeof(IDesignTimeDbContextFactory<>).MakeGenericType(contextType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ public void CreateContext_gets_service_without_AddDbContext()
CreateOperations(typeof(TestProgramWithoutAddDbContext)).CreateContext(typeof(TestContext).FullName);
}

[ConditionalFact]
public void CreateContext_gets_service_when_context_factory_used()
{
CreateOperations(typeof(TestProgramWithContextFactory)).CreateContext(typeof(TestContextFromFactory).FullName);
}

[ConditionalFact]
public void Can_pass_null_args()
{
Expand Down Expand Up @@ -119,6 +125,15 @@ private static TestWebHost BuildWebHost(string[] args)
.BuildServiceProvider());
}

private static class TestProgramWithContextFactory
{
private static TestWebHost BuildWebHost(string[] args)
=> new TestWebHost(
new ServiceCollection()
.AddDbContextFactory<TestContextFromFactory>(b => b.UseInMemoryDatabase("In-memory test database"))
.BuildServiceProvider());
}

private static class TestProgramRelational
{
private static TestWebHost BuildWebHost(string[] args)
Expand Down Expand Up @@ -163,6 +178,19 @@ public TestContext(DbContextOptions<TestContext> options)
}
}

private class TestContextFromFactory : DbContext
{
private TestContextFromFactory()
{
throw new Exception("This isn't the constructor you're looking for.");
}

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

private class BaseContext : DbContext
{
public BaseContext(string factoryUsed)
Expand Down

0 comments on commit 343a23a

Please sign in to comment.