question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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”

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:26

github_iconTop GitHub Comments

59reactions
cemgcommented, Mar 4, 2022

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.

public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
        public ApplicationDbContext CreateDbContext(string[] args)
        {
            var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
            optionsBuilder.UseSqlServer("Server=(localdb)\\MSSQLLocalDB;Database=EcommerceDb;Trusted_Connection=True;MultipleActiveResultSets=true");

            return new ApplicationDbContext(optionsBuilder.Options);
        }
}

IDE: VS 2022 Pro Framework: .NET6 Packages Version: 6.0.2

15reactions
cemgcommented, May 20, 2022

You can use ConfigurationBuilder class for build the configuration.

public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
    public ApplicationDbContext CreateDbContext(string[] args)
    {
        var configuration = new ConfigurationBuilder()
            .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
            .AddJsonFile("appsettings.json")
            .Build();
        
        var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
        optionsBuilder.UseSqlServer(configuration.GetConnectionString("Default"));

        return new ApplicationDbContext(optionsBuilder.Options);
    }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found