Scaffolding doesn't work when DbContext is in a separate project
- Dominant language
- C#
- Stars
- 818
- Forks
- 260
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 10
Description
Consistently getting this error when attempting to scaffold a controller while having DbContext in a separate project:
```
There was an error running selected code generator:
'Unable to resolve service for type
'Microsoft.EntityFrameworkCore.DbContextOptions`1[MyProject.Data.MyProjectDbContext]'
while attempting to activate 'MyProject.Data.MyProjectDbContext'.'
```
MyProjectDbContext:
```
public class MyProjectDbContext : DbContext
{
public MyProjectDbContext(DbContextOptions options) : base (options)
{ }
public DbSet Notes => Set();
}
```
DbContext is registered in Program.cs as follows:
```
builder.Services.AddDbContext(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("MyProjectDbContext")));
```
Scaffolding **works** if DbContext moved to the web project.
IDE: Visual Studio 2022 Community
SDK: .NET 6.0
Packages:
"Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.1"
"Microsoft.EntityFrameworkCore.Tools" Version="6.0.1"
"Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.1"
Contributor guide
Assessment
This issue has not been assessed yet.