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

Design tools unable create DbContext with custom constructor using the services provider #17273

Merged
merged 2 commits into from
Jan 29, 2020
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
4 changes: 2 additions & 2 deletions src/EFCore.Design/Design/Internal/DbContextOperations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ where i.IsGenericType
{
try
{
return (DbContext)Activator.CreateInstance(context);
return (DbContext)ActivatorUtilities.GetServiceOrCreateInstance(appServices, context);
}
catch (MissingMethodException ex)
catch (Exception ex)
{
throw new OperationException(DesignStrings.NoParameterlessConstructor(context.Name), ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public void CreateContext_gets_service()
CreateOperations(typeof(TestProgram)).CreateContext(typeof(TestContext).FullName);
}

[ConditionalFact]
public void CreateContext_gets_service_without_AddDbContext()
{
CreateOperations(typeof(TestProgramWithoutAddDbContext)).CreateContext(typeof(TestContext).FullName);
}

[ConditionalFact]
public void GetContextInfo_returns_correct_info()
{
Expand Down Expand Up @@ -67,6 +73,20 @@ private static TestWebHost BuildWebHost(string[] args)
=> CreateWebHost(b => b.UseInMemoryDatabase("In-memory test database"));
}

private static class TestProgramWithoutAddDbContext
{
private static TestWebHost BuildWebHost(string[] args)
=> new TestWebHost(
new ServiceCollection()
.AddSingleton(
new TestContext(
new DbContextOptionsBuilder<TestContext>()
.UseInMemoryDatabase("In-memory test database")
.EnableServiceProviderCaching(false)
.Options))
.BuildServiceProvider());
}

private static class TestProgramRelational
{
private static TestWebHost BuildWebHost(string[] args)
Expand Down