Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add DbContext Model diagram - custom DbContext cannot be activated (EF Core 5) #690

Closed
alecoronel opened this issue Dec 29, 2020 · 3 comments

Comments

@alecoronel
Copy link

We are trying out EFCore Power Tools for the first time in our .Net5 project with EF Core 5.

The project is able to use migrations and everything related to EF Core works fine, but alas I cannot use EF Core Power Tools since we are always getting the error below (when trying to add DbContext Model diagram via EFCore Power Tools) no matter how much we refactor the code and try different approaches regarding the DbContext DI setup.

The error is this;

System.ArgumentException: Error:
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'MyDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
---> System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[MyProject.DataAccess.MyDbContext]' while attempting to activate 'MyProject.DataAccess.MyDbContext'.

Steps to reproduce

Create a custom class derived from DbContext as usual. Register it in the ServiceCollection in Startup.ConfigureServices as recommended;

            var connection = new SqlConnection(Configuration.GetConnectionString("DefaultConnection"));
            var builder = new DbContextOptionsBuilder<MyDbContext>().UseSqlServer(connection);

            services.AddScoped<DbContextOptions<MyDbContext>>(_ => builder.Options);
            services.AddDbContext<MyDbContext, MyDbContext>(); // As AddDbContext for design time tooling to find it automatically

In Program.cs set it all up as you always do;

        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    
         // (etc...)

Further technical details

EF Core Power Tools version: 2.5.406.0

Database engine: SQL Server

Visual Studio version: 2019 - 16.8.3

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 29, 2020

Possible duplicate of this: #655 (comment)

@alecoronel
Copy link
Author

Perfect, thanks, it was the exact same issue!

@ErikEJ
Copy link
Owner

ErikEJ commented Dec 29, 2020

I have updated the docs here: https://github.com/ErikEJ/EFCorePowerTools/wiki/Inspect-your-DbContext-model

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants