Scaffolding: Scaffolding doesn't work when DbContext is in a separate project
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<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”
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 12
- Comments: 29
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.
Did you find a solution? I’m having the same issue.
The migrations are working fine, but Scaffolding wont work. Only slight difference is I am using IdentityDbContext.
Thanks Cem, nice workaround though, its working! Teşekkürler.
I believe that your problem has come from elsewhere, microsoft itself recommends building a db context factory.
It was useful for me but the SetBasePath method throwed me an error. I fix it adding two nugget packages:
Microsoft.Extensions.Configuration.FileExtensions Microsoft.Extensions.Configuration.Json
The answer was commented on: https://stackoverflow.com/questions/36001695/setting-base-path-using-configurationbuilder
Although this resolved the issue for me, it introduced some side effects which I only realized weeks later. It changes some of the internals of identity classes, see the following migration:
Even though nothing else was changed in the project (related to the database schema), when your proposed class is present, these columns are changed when trying to create a new migration. Removing the class and rescaffolding the migration results in an empty migration.
Setting the
MaxLengthForKeysproperty manually in the relevantProgram.cs(as mentioned here) changes nothing.Not sure where this comes from but maybe this is useful information to someone.
Thanks @cemg. Found this after hours of testing. Works great
@cemg Thanks! This also works for me.