title: .NET 6 Single-File ASP.NET template with Entity Framework Core
body: <p>I am using the Visual Studio 2022/.NET 6 Razor pages template. It uses a top-level C# 10 program file. Program.cs looks something like this:</p>
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.EntityFrameworkCore;
  using Data;
  using Services;
  
  var builder = WebApplication.CreateBuilder(args);
  
  builder.Services.AddMemoryCache();
  builder.Services.AddRazorPages();
  
  var settings = builder.Configuration.Get&lt;AppSettings&gt;();
  
  builder.Services
      .AddDbContext&lt;ApplicationDbContext&gt;(options =&gt; options.UseSqlServer(builder.Configuration.GetConnectionString(&quot;DefaultConnection&quot;)));
  
  var app = builder.Build();
  app.UseHttpsRedirection();
  app.UseRouting();
  app.MapRazorPages();
  app.Run();
  
  </code></pre>
  <p>My DBContext looks like this</p>
  <pre class="lang-cs prettyprint-override"><code>public class ApplicationDbContext: DbContext
  {
      public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options) { }
      internal DbSet&lt;MyModel&gt; MyModels { get; set; }
      protected override void OnModelCreating(ModelBuilder builder){ /* ... */ }
  }
  </code></pre>
  <p>When I run <code>dotnet ef migrations add InitialMigration -v</code>, I get the following error:</p>
  <pre><code>Finding DbContext classes...
  Finding IDesignTimeDbContextFactory implementations...
  Finding application service provider in assembly 'Data'...
  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 'ApplicationDbContext'.
  Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'ApplicationDbContext'. 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[Data.DatabaseContext.ApplicationDbContext]' while attempting to activate 'Data.DatabaseContext.ApplicationDbContext'.
  </code></pre>
  <p>Visual Studio <code>Add-Migration</code> also fails to build, but provides no stack trace.</p>
  <p>The <a href="https://go.microsoft.com/fwlink/?linkid=851728" rel="nofollow noreferrer">provided link</a> indicated that I might be able to add an implementation of <code>IDesignTimeDbContextFactory</code> to contain some startup code that might work.</p>
  <p>I created this class, but it also seems to do nothing.</p>
  <pre class="lang-cs prettyprint-override"><code>public class ApplicationContext: IDesignTimeDbContextFactory&lt;ApplicationDbContext&gt;
  {
      ApplicationDbContext IDesignTimeDbContextFactory&lt;SanDiegoContext&gt;.CreateDbContext(string[] args)
      {
          IConfigurationRoot configuration = MyStaticConfigurationBuilder.GetConfig();
  
          var optionsBuilder = new DbContextOptionsBuilder&lt;ApplicationDbContext&gt;();
          optionsBuilder.UseSqlServer(connectionString: configuration.GetConnectionString(&quot;DefaultConnection&quot;));
  
          return new ApplicationDbContext(optionsBuilder.Options);
      }
  }
  </code></pre>
  <p>Is it possible to use EF Core in .NET 6 without building a <code>GetHostBuilder(string[])</code> method and a <code>Startup</code> class? If so, what am I doing wrong?</p>