Scaffolding doesn't work when DbContext is in a separate project
See original GitHub issueConsistently 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<MyProjectDbContext> options) : base (options)
{ }
public DbSet<Note> Notes => Set<Note>();
}
DbContext is registered in Program.cs as follows:
builder.Services.AddDbContext<MyProjectDbContext>(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”
Issue Analytics
- State:
- Created 2 years ago
- Reactions:11
- Comments:26
Top Results From Across the Web
visual studio 2022 .NET 6 Scaffolding doesn't work when ...
First make sure you have the correct packages in the same project as the context. ... Use this OB to scaffold optionsBuilder.
Read more >Entity Framework Core Scaffold-DbContext in separate Data ...
In the practical scenario in that Project Solution may have different layers to manage and perform defined operations.
Read more >Scaffolding Error when Model and DbContext are in ...
Scaffolding views with VS2017 v15.4.1 and entity framework core 2.0 fails when my model and data classes are in separate class libraries.
Read more >Scaffolding (Reverse Engineering) - EF Core
Reverse engineering is the process of scaffolding entity type classes and a DbContext class based on a database schema. It can be performed ......
Read more >Scaffold Existing Database - Entity Framework Core - YouTube
In this video, we explore generating a Code First Database model from an existing database. Sometimes, doing code first is not an option, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I found a solution. You can create a dbcontext factory class in same folder with your ApplicationDbContext class. This factory class creates ApplicationDbContext at design time and scaffolding runs correctly.
IDE: VS 2022 Pro Framework: .NET6 Packages Version: 6.0.2
You can use ConfigurationBuilder class for build the configuration.