OrchardCore: [Solution included] Unable to add the EntityFramework Core migration
Describe the bug
When I tried to add initial Entity Framework Core migration it couldn’t create the database context.
To Reproduce
Steps to reproduce the behaviour:
- Create a solution with name
Test. - Create the web application project in the solution with name
WebAppfrom the latestdevbranch using the following commands:
dotnet new -i "OrchardCore.ProjectTemplates::1.0.0-rc2-*" --nuget-source https://nuget.cloudsmith.io/orchardcore/preview/v3/index.json
dotnet new occms
- Create the .NET Core 5 library with name
Core; - Add EntityFramework Core packages to both projects.
- Create the EF Core database context
ApplicationDbContextin theCoreproject and one entity type. - Try to create the initial migration. Run the following command in the solution’s directory:
dotnet ef migrations add Initial -c ApplicationDbContext -s .\WebApp -v -p .\Core
- Get the error:
Unable to create an object of type 'ApplicationDbContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
Expected behavior
Migration will be created successfully.
Screenshots

Solution
The problem was in the Program.cs. I renamed the BuildHost static method to CreateHostBuilder and changed the return type to IHostBuilder.
Before:
Program.cs
public class Program
{
public static Task Main(string[] args)
=> BuildHost(args).RunAsync();
public static IHost BuildHost(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging => logging.ClearProviders())
.ConfigureWebHostDefaults(webBuilder => webBuilder
.UseStartup<Startup>()
.UseNLogWeb())
.Build()
;
}
After:
Program.cs
public class Program
{
public static Task Main(string[] args)
=> CreateHostBuilder(args).Build().RunAsync();
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging => logging.ClearProviders())
.ConfigureWebHostDefaults(webBuilder => webBuilder
.UseStartup<Startup>()
.UseNLogWeb())
;
}
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 15 (10 by maintainers)
Commits related to this issue
- Merge branch 'main' of https://github.com/OrchardCMS/OrchardCore into ns8482e/#9882 — committed to surevelox/OrchardCore by ns8482e 3 years ago
For my specific use case, for connection strings, I use command line with arguments and manage my uid/pwd in secret manager.