Scaffolding controller with EF context fails when context and the DI container live in different projects
- Dominant language
- C#
- Stars
- 818
- Forks
- 260
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 10
Description
Scaffolding controller with EF context fails when context and the DI container live in different projects.
[Repro project](https://github.com/kszicsillag/ScaffoldingTest)
[master branch](https://github.com/kszicsillag/ScaffoldingTest/tree/master) version fails to get an EF context instance from DI.
[singleproj branch](https://github.com/kszicsillag/ScaffoldingTest/tree/singleproj) version is functionally identical, only the projects merged as one. Scaffolding works as expected.
Scaffolding command: `dotnet aspnet-codegenerator controller -m Library1.Models.Car -dc DAL.CarContext -outDir Controllers -name EFCarController -namespace Api.Controllers -api`
Result on master branch:
```
Finding the generator 'controller'...
Running the generator 'controller'...
Minimal hosting scenario!
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Car'
Unable to create an object of type 'CarContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 StackTrace:
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.b__13()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
at Microsoft.EntityFrameworkCore.Design.DbContextActivator.CreateInstance(Type contextType, Assembly startupAssembly, IOperationReportHandler reportHandler, String[] args)
at Microsoft.EntityFrameworkCore.Design.DbContextActivator.CreateInstance(Type contextType, Assembly startupAssembly, IOperationReportHandler reportHandler)
at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkModelProcessor.TryCreateContextUsingAppCode(Type dbContextType, Type startupType)
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[DAL.CarContext]' while attempting to activate 'DAL.CarContext'. StackTrace:
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters)
at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.b__13()
Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[DAL.CarContext]' while attempting to activate 'DAL.CarContext'.
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.b__6_0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
RunTime 00:00:09.97
```
### Possible cause
Managed to track down a potential cause to [this line](https://github.com/dotnet/Scaffolding/blob/e0f24482ca7d6c0ebbd96e124c3e933d705d8221/src/Scaffolding/VS.Web.CG.EFCore/EntityFrameworkModelProcessor.cs#L513). The second parameter should be a type from the startup project, but the EF context type is passed instead.
### Provider and version information
Microsoft.VisualStudio.Web.CodeGeneration.Design v6.0.3
Target framework: .NET 6.0
Operating system: Windows 10
IDE: Visual Studio 2022 17.1.1
Contributor guide
Assessment
This issue has not been assessed yet.