title: Microsoft.EntityFrameworkCore.Tools - Azure Functions with .NET 5 (Isolated) Design-time DbContext Creation with HostBuilder
body: <p>I have created a new <code>FunctionApp</code> in <code>Visual Studio Version 16.10.0</code> using the template <code>Azure Functions</code> with <code>.NET 5 (Isolated)</code> and <code>Http trigger</code>.</p>
  <p><a href="https://i.stack.imgur.com/sEUYb.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sEUYb.png" alt="enter image description here" /></a></p>
  <p>I have then created the following files:</p>
  <p>Blog.cs</p>
  <pre><code>public class Blog
  {
      public int Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  <p>ApplicationDbContext.cs</p>
  <pre><code>public class ApplicationDbContext : DbContext
  {
      public ApplicationDbContext(
          DbContextOptions&lt;ApplicationDbContext&gt; options) : base(options)
      {
      }
  
      public DbSet&lt;Blog&gt; Blogs { get; set; }
  }
  </code></pre>
  <p>Program.cs</p>
  <pre><code>public class Program
  {
      public static void Main()
      {
          var host = new HostBuilder()
              .ConfigureFunctionsWorkerDefaults()
              .ConfigureServices(services =&gt;
              {
                  var connectionString = Environment.GetEnvironmentVariable(&quot;ConnectionStrings:DefaultConnection&quot;);
                  services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                          options.UseSqlServer(
                          connectionString,
                          sqlServerOptions =&gt; sqlServerOptions.CommandTimeout(600)));
              })
              .Build();
  
          host.Run();
      }
  }
  </code></pre>
  <p>Function1.cs</p>
  <pre><code>public class Function1
  {
      private readonly ApplicationDbContext _applicationDbContext;
  
      public Function1(ApplicationDbContext applicationDbContext)
      {
          _applicationDbContext = applicationDbContext;
      }
  
      [Function(&quot;Function1&quot;)]
      public HttpResponseData Run([HttpTrigger(AuthorizationLevel.Function, &quot;get&quot;, &quot;post&quot;)] HttpRequestData req,
          FunctionContext executionContext)
      {
          var logger = executionContext.GetLogger(&quot;Function1&quot;);
          logger.LogInformation(&quot;C# HTTP trigger function processed a request.&quot;);
  
          var connectionString = _applicationDbContext.Database.GetDbConnection().ConnectionString;
  
          var response = req.CreateResponse(HttpStatusCode.OK);
          response.Headers.Add(&quot;Content-Type&quot;, &quot;text/plain; charset=utf-8&quot;);
  
          response.WriteString(&quot;Welcome to Azure Functions!&quot;);
  
          return response;
      }
  }
  </code></pre>
  <p>local.settings.json:</p>
  <pre><code>{
    &quot;ConnectionStrings&quot;: {
      &quot;DefaultConnection&quot;: &quot;Server=(localdb)\\mssqllocaldb;Database=FunctionApp1.Test;Trusted_Connection=True;MultipleActiveResultSets=true&quot;
    },
    &quot;IsEncrypted&quot;: false,
    &quot;Values&quot;: {
      &quot;AzureWebJobsStorage&quot;: &quot;UseDevelopmentStorage=true&quot;,
      &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;dotnet-isolated&quot;
    }
  }
  </code></pre>
  <p>host.json</p>
  <pre><code>{
      &quot;version&quot;: &quot;2.0&quot;,
      &quot;logging&quot;: {
          &quot;applicationInsights&quot;: {
              &quot;samplingSettings&quot;: {
                  &quot;isEnabled&quot;: true,
                  &quot;excludedTypes&quot;: &quot;Request&quot;
              }
          }
      }
  }
  </code></pre>
  <p>I have verified that dependency injection works from <code>Program.cs</code> -&gt; <code>new HostBuilder</code></p>
  <p><a href="https://i.stack.imgur.com/gnzEM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gnzEM.png" alt="enter image description here" /></a></p>
  <p>If I run <code>Add-Migration InitialCreate</code> in Package Manager Console I get the following error:</p>
  <blockquote>
  <p>Unable to create an object of type 'ApplicationDbContext'. For the
  different patterns supported at design time, see
  <a href="https://go.microsoft.com/fwlink/?linkid=851728" rel="nofollow noreferrer">https://go.microsoft.com/fwlink/?linkid=851728</a></p>
  </blockquote>
  <p><a href="https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/?tabs=vs</a></p>
  <p><a href="https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=vs" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/ef/core/cli/dbcontext-creation?tabs=vs</a></p>
  <p>To obtain the DbContext object from the application's service provider the example code looks like this:</p>
  <pre><code>public class Program
  {
      public static void Main(string[] args)
          =&gt; CreateHostBuilder(args).Build().Run();
  
      // EF Core uses this method at design time to access the DbContext
      public static IHostBuilder CreateHostBuilder(string[] args)
          =&gt; Host.CreateDefaultBuilder(args)
              .ConfigureWebHostDefaults(
                  webBuilder =&gt; webBuilder.UseStartup&lt;Startup&gt;());
  }
  
  public class Startup
  {
      public void ConfigureServices(IServiceCollection services)
          =&gt; services.AddDbContext&lt;ApplicationDbContext&gt;();
  
      public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
      {
      }
  }
  
  public class ApplicationDbContext : DbContext
  {
      public ApplicationDbContext(DbContextOptions&lt;ApplicationDbContext&gt; options)
          : base(options)
      {
      }
  }
  </code></pre>
  <blockquote>
  <p>The tools first try to obtain the service provider by invoking
  Program.CreateHostBuilder(), calling Build(), then accessing the
  Services property.</p>
  </blockquote>
  <p>Is there anyway to make Tools work with HostBuilder instead?</p>