dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.79k stars 3.19k forks source link

DbContextOperations.CreateContext(String contextType) seems to behave inconsistenly #13563

Closed seancpeters closed 2 years ago

seancpeters commented 6 years ago

Describe what is not working as expected. I'm getting seemingly inconsistent behavior when calling Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) via aspnet/scaffolding of Razor Pages (CRUD) for a 2.1 project. If you are seeing an exception, include the full exceptions details (message and stack trace).

It seems to work fine in most cases - these cases work:

The combination which fails is:

The difference between the projects would be that the individual auth projects already contain an IdentityDbContext, but this scaffolding gesture would create a DbContext-derived class.

Exception message:
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'CarModel'
Unable to create an object of type 'MyContext'. Add an implementation of 'IDesignTimeDbContextFactory<MyContext>' to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time. StackTrace:No parameterless constructor defined for this object.

Stack trace:
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_3.<FindContextTypes>b__13()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
(this is where the stack track starts in EF packages. There is more stack from aspnet/scaffolding, but that should be irrelevant here.

Steps to reproduce

Include a complete code listing (or project/solution) that we can run to reproduce the issue.

Partial code listings, or multiple fragments of code, will slow down our response or cause us to push the issue back to you to provide code to reproduce the issue.

I'm attaching 4 sample projects, each in their own zip. They're named to reflect the 4 cases mentioned above.

Core21IndAuth.zip Core21NoAuth.zip Framework21IndAuth.zip Framework21NoAuth.zip

Further technical details

EF Core version: 2.1.4 Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.1.4 Operating system: Windows IDE: Visual Studio 2017 15.9.0 Preview 3.0 [28128.56.d15.9]

ajcvickers commented 6 years ago

@seancpeters Can you provide some details as to why you are calling EF internal APIs?

bricelam commented 6 years ago

The public API to do this is DbContextActivator.CreateInstance()

seancpeters commented 6 years ago

The call to this API is part of aspnet/scaffolding, here: https://github.com/aspnet/Scaffolding/blob/master/src/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs# At line 404.

I don't know the historical context of why scaffolding is creating db contexts in this manner, as opposed to some other way.

bricelam commented 6 years ago

Good time to fix aspnet/Scaffolding#681 and see if it still fails?

seancpeters commented 6 years ago

@bricelam - Thank you! This appears to have fixed the problem. Looking at history, it seems that the original fix never got merged, or got lost somewhere along the way.