dotnet / dotnet/Scaffolding

Scaffolding doesn't work when DbContext is in a separate project

Open
#1,765 36 comments 16 reactions 0 assignees View on GitHub
0 - Backlog area-scaffolding customer-reported Feature Triage-Approved
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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.