[ACCEPTED]-Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions'-entity-framework-core
In my experience, better to create a class 5 (in the same project as the context) that 4 implements IDesignTimeDbContextFactory - as 3 described here. That way, there's not guessing 2 as to which code will get used when using 1 the design time tools such as update-database
Thanks to Ivan and Kirk's comments above 7 and reading the entire verbose output, I 6 figured out the problem. Turned out I was 5 not following the correct pattern in Program.cs
.
From 4 the documentation,
The tools first try to obtain the service provider by invoking Program.CreateHostBuilder(), calling Build(), then accessing the Services property.
I had refactored the original Program.cs
by moving 3 CreateHostBuilder()
inside main()
, which broke the ef-core migration.
After 2 modifying the Program.cs
to following it works as 1 expected.
public class Program
{
public static void Main(string[] args)
=> CreateHostBuilder(args).Build().Run();
// EF Core uses this method at design time to access the DbContext
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(
webBuilder => webBuilder.UseStartup<Startup>());
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.