fullstackhero / blazor-starter-kit

Clean Architecture Template for Blazor WebAssembly Built with MudBlazor Components.
MIT License
3.48k stars 730 forks source link

Unable to run migrations on initial setup #349

Open thompcd opened 2 years ago

thompcd commented 2 years ago

When I attempt to setup my database for the first time I am getting errors which I am not certain whether they are due to code issues or configuration.

I've followed the readme and blog post (https://codewithmukesh.com/blog/blazor-hero-quick-start-guide/) to get started on both Mac and Windows and I receive the same results in both OS.

I've looked onto stack overflow and saw a suggestion to add empty constructors on the contexts to test if the context injection was the issue which produced no results when adding empty constructors to both AuditableContext and BlazorHeroContext.

I've also tried injecting the contexts during ConfigureServices and still receive the same error.

Following the MS error link from the console output takes me to a dead page with a notification that what I'm looking for has moved.

Additionally, I have tried cloning the source code directly and receive the same issue.

To Reproduce cd ../src/Infrastructure Set startup project --> Infrastructure.csproj update-database OR cd ../src/Infrastructure dotnet ef -v --startup-project ./Infrastructure.csproj migrations add Migration001

output:

CleanArchitecture/src/Infrastructure on  master via .NET v5.0.402 🎯 net5.0 took 24s 
❯ dotnet ef -v --startup-project ./Infrastructure.csproj migrations add Migration001
Using project '/Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/Infrastructure.csproj'.
Using startup project './Infrastructure.csproj'.
Writing '/Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/obj/Infrastructure.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/fl/8xdqwf9n2tjc0qpts580c0tw0000gn/T/tmpXeOtfo.tmp /verbosity:quiet /nologo /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/Infrastructure.csproj
Writing './obj/Infrastructure.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/var/folders/fl/8xdqwf9n2tjc0qpts580c0tw0000gn/T/tmpndDILL.tmp /verbosity:quiet /nologo ./Infrastructure.csproj
Build started...
dotnet build ./Infrastructure.csproj /verbosity:quiet /nologo

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:07.38
Build succeeded.
dotnet exec --depsfile /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/bin/Debug/net5.0/BlazorHero.CleanArchitecture.Infrastructure.deps.json --additionalprobingpath /Users/corey/.nuget/packages --runtimeconfig /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/bin/Debug/net5.0/BlazorHero.CleanArchitecture.Infrastructure.runtimeconfig.json /Users/corey/.dotnet/tools/.store/dotnet-ef/5.0.11/dotnet-ef/5.0.11/tools/netcoreapp3.1/any/tools/netcoreapp2.0/any/ef.dll migrations add Migration001 --assembly /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/bin/Debug/net5.0/BlazorHero.CleanArchitecture.Infrastructure.dll --startup-assembly /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/bin/Debug/net5.0/BlazorHero.CleanArchitecture.Infrastructure.dll --project-dir /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/ --language C# --working-dir /Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure --verbose --root-namespace BlazorHero.CleanArchitecture.Infrastructure
Using assembly 'BlazorHero.CleanArchitecture.Infrastructure'.
Using startup assembly 'BlazorHero.CleanArchitecture.Infrastructure'.
Using application base '/Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/bin/Debug/net5.0'.
Using working directory '/Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure'.
Using root namespace 'BlazorHero.CleanArchitecture.Infrastructure'.
Using project directory '/Users/corey/Code/LineHubDesigner/CleanArchitecture/src/Infrastructure/'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'BlazorHero.CleanArchitecture.Infrastructure'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'BlazorHeroContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'BlazorHeroContext'. 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[BlazorHero.CleanArchitecture.Infrastructure.Contexts.BlazorHeroContext]' while attempting to activate 'BlazorHero.CleanArchitecture.Infrastructure.Contexts.BlazorHeroContext'.
   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__DisplayClass13_4.<FindContextTypes>b__13()
   --- End of inner exception stack trace ---
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass13_4.<FindContextTypes>b__13()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Unable to create an object of type 'BlazorHeroContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728

Expected behavior Once the database migration is done, you will be able to see the newly created database in the server explorer. Default Data like Roles and Users will be seeded into the database when you run the application for the first time.

Desktop (please complete the following information):

Additional context Fresh install of VS2019 for Mac on my Mac, pulled latest template of BlazorHero as of 10/22/21 (uncertain how to see version)

jeremiedevos commented 2 years ago

From a quick search I came across someone who had the same issue because the startup project was not correct. As mentioned in the https://codewithmukesh.com/blog/blazor-hero-quick-start-guide/ the startup project should be the api (web/server/server.cproj) project and not Infrastructure.csproj unless you mean it is the default project in the package manager.