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:

  1. Create a solution with name Test.
  2. Create the web application project in the solution with name WebApp from the latest dev branch 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
  1. Create the .NET Core 5 library with name Core;
  2. Add EntityFramework Core packages to both projects.
  3. Create the EF Core database context ApplicationDbContext in the Core project and one entity type.
  4. 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
  1. 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

image

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

Most upvoted comments

For my specific use case, for connection strings, I use command line with arguments and manage my uid/pwd in secret manager.