title: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions'
body: <p>I am trying to access the databse using EF Core 5.0 on an ASP.NET Core project. For the first migration, I overrode the <code>OnConfiguring()</code> method on the DBContext and updated the database successfully.</p>
  <p>For the second migration, I decided to use the dependency injection in ASP.NET Core following the <a href="https://docs.microsoft.com/en-gb/ef/core/cli/dbcontext-creation?tabs=dotnet-core-cli#from-application-services" rel="noreferrer">guidelines</a>. Here are the changes I made.</p>
  <ol>
  <li>Added <code>services.AddDbContext</code> in my <code>Startup.cs</code>.</li>
  <li>Removed the <code>OnConfiguring()</code> method from <code>DBContext</code>.</li>
  </ol>
  <p>After running <code>dotnet ef migrations add Posts</code>, I get following error:</p>
  <pre><code>Microsoft.EntityFrameworkCore.Design.OperationException: 
  Unable to create an object of type 'BlogContext'. 
  For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
  </code></pre>
  <p>If I add the --verbose flag, I get this output:</p>
  <pre><code>Build started...
  dotnet build blog/app/app.csproj /verbosity:quiet /nologo
  
  Build succeeded.
      0 Warning(s)
      0 Error(s)
  
  Time Elapsed 00:00:02.50
  Build succeeded.
  
  Finding DbContext classes...
  Finding IDesignTimeDbContextFactory implementations...
  Finding application service provider in assembly 'app'...
  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 'BlogContext'.
  Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
   ---&gt; System.InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[app.Data.BlogContext]' while attempting to activate 'app.Data.BlogContext'.
     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.&lt;&gt;c__DisplayClass13_4.&lt;FindContextTypes&gt;b__13()
     
  Unable to create an object of type 'BlogContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
   
  </code></pre>
  <p>However, the code works as expected when I run the web application, as in the <code>BlogContext</code> is created and injected into my classes by the DI layer, and I can access the database.</p>
  <p>Hence, I am guessing the DI layer is not running as expected when running the <code>dotnet ef migrations add</code> command.</p>
  <p>Here's my code.</p>
  <pre><code>// Program.cs
  
  public class Program
  {
      public static void Main(string[] args)
      {
          IHostBuilder builder = Host.CreateDefaultBuilder(args)
                                          .UseSerilog()
                                          .ConfigureWebHostDefaults(webBuilder =&gt;
                                          {
                                              webBuilder.UseStartup&lt;Startup&gt;();
                                          });
  
          IHost host = builder.Build();
  
          host.Run();
      }
  }
  
  // BlogContext
  
  using app.Models;
  using Microsoft.EntityFrameworkCore;
  
  namespace app.Data
  {
      public class BlogContext : DbContext
      {
          public DbSet&lt;Post&gt; Posts { get; set; }
  
          public DbSet&lt;Comment&gt; Comments { get; set; }
  
          public BlogContext(DbContextOptions&lt;BlogContext&gt; options) : base(options)
          {
  
          }
      }
  }
  
  // Startup.cs
  
  using Microsoft.EntityFrameworkCore;
  using Microsoft.Extensions.DependencyInjection;
  
  public class Startup
  {
      public Startup(IConfiguration configuration)
      {
           Configuration = configuration;
      }
  
      public IConfiguration Configuration { get; }
  
      public string ConnectionString =&gt; Configuration.GetConnectionString(&quot;Blog&quot;);
  
      public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;BlogContext&gt;(opt =&gt; { opt.UseSqlite(ConnectionString); });
      }
  }
  </code></pre>
  <p>Both the <code>Startup</code> and <code>BlogContext</code> live in the same project.</p>