ardalis / CleanArchitecture

Clean Architecture Solution Template: A starting point for Clean Architecture with ASP.NET Core
MIT License
16.14k stars 2.78k forks source link

Error when using EF Core Power Tools #604

Open hddhagai opened 11 months ago

hddhagai commented 11 months ago

Always keep getting this error when using EF Core Power Tools.

System.InvalidOperationException: Error: Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'AppDbContext'. 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.DbContextOptions1[Ex.Infrastructure.Data.AppDbContext]' while attempting to activate 'Ex.Infrastructure.Data.AppDbContext'. at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.ConstructorMatcher.CreateInstance(IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.CreateInstance(IServiceProvider provider, Type instanceType, Object[] parameters) at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetServiceOrCreateInstance(IServiceProvider provider, Type type) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13() --- End of inner exception stack trace --- at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass21_4.<FindContextTypes>b__13() at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Modelling.EfCoreModelBuilder.BuildResult(String outputPath, String startupOutputPath, Boolean generateDdl) in C:\Code\EFCorePowerTools\src\GUI\efpt30.core\EFCoreModelBuilder.cs:line 41 at Modelling.Program.Main(String[] args) in C:\Code\EFCorePowerTools\src\GUI\efpt30.core\Program.cs:line 56

at async Task EFCorePowerTools.Handlers.ModelAnalyzerHandler.GenerateAsync(string outputPath, Project project, GenerationType generationType)

I have Multiple start up projects. Have tried with just the web as the start up project still get the error. Is there something that I am missing? Am new to Clean Architecture

fbjerggaard commented 9 months ago

Did you change the database provider? I encountered this when I switched to npgsql, however setting the connection string to the correct format fixed this error.

yogyogi commented 6 months ago

This is how I run the repo:

  1. Only the "Web" project is the startup project.
  2. Also delete the database file given inside the "Web" project. This is because once you run the app the database will be automatically created so you don't have to run the migrations.

Also to tell that I have added Identity on this repo then done the migrations, it worked for me. The description of this repo contains the migration command which I ran. The migration command has to run from the directory of the "web" project because here you have the project file of the "web" project.