title: Established dependency injection in .NET Core Console Application for EF Core DbContext Service Error
body: <p>I've created a .net Core Console Application, and I've added the following dependency injection package:</p>
  <blockquote>
  <p>Microsoft.Extensions.DependencyInjection</p>
  </blockquote>
  <p>To inject EF Core DbContext Service, Here's a code snippet of the project:</p>
  <pre><code>static void Main(string[] args)
          {
              // Create service collection and configure our services
              var services = ConfigureServices();
  
              // Generate a provider
              var serviceProvider = services.BuildServiceProvider();
  
              // Kick off our actual code
              serviceProvider.GetService&lt;Startup&gt;().Run();
          }
  
          public static IConfiguration LoadConfiguration()
          {
              var builder = new ConfigurationBuilder()
                  .SetBasePath(Directory.GetCurrentDirectory())
                  .AddJsonFile(&quot;appsettings.json&quot;, optional: false, reloadOnChange: true)
                  .AddJsonFile($&quot;appsettings.{Environment.GetEnvironmentVariable(&quot;ASPNETCORE_ENVIRONMENT&quot;) ?? &quot;Production&quot;}.json&quot;, optional: true)
                  .AddEnvironmentVariables();
  
              return builder.Build();
          }
  
  
  private static IServiceCollection ConfigureServices()
          {
              IServiceCollection services = new ServiceCollection();
  
              // Set up the objects we need to get to configuration settings
              var configuration = LoadConfiguration();
  
  
              // IMPORTANT! Register our cvonfig file, db connection string, and application entry point(startup)
              services
                  .AddSingleton(configuration)
                  .AddConnection(configuration)
                  .AddStartup();
              
              return services;
          }
  </code></pre>
  <p>Services Pool CLass:</p>
  <pre><code>  public static class ServicesPool
      {
          public static IServiceCollection AddStartup(this IServiceCollection services)
          {
              services.AddTransient&lt;Startup&gt;();
  
              return services;
          }
  
          public static IServiceCollection AddConfiggguration(this IServiceCollection services, IConfiguration configuration)
          {
              services.AddSingleton(configuration);
  
              return services;
          }
  
          public static IServiceCollection AddConnection(this IServiceCollection services, IConfiguration configuration)
          {
              var connection = configuration.GetConnectionString(&quot;DEV_CS&quot;);
              services.AddDbContext&lt;MigrationDbContext&gt;(options =&gt;
              options.UseSqlServer(connection, b =&gt; b.MigrationsAssembly(&quot;migration.presentence&quot;)));
  
              return services;
          }
  
      }
  </code></pre>
  <p>MigrationDbContext Service that implements the DbContext from EntityFramwork Core:</p>
  <pre><code>public class MigrationDbContext : DbContext
      {
          public MigrationDbContext(DbContextOptions dbContextOptions) : base(dbContextOptions)
          {
          }
  
          public DbSet&lt;RootItemMigrationEntity&gt; RootItems { get; set; }
  
      }
  </code></pre>
  <p>The application runs without any problem, but when i try to create the inital migration using the following command:</p>
  <blockquote>
  <p>Add-Migration 'Initial'</p>
  </blockquote>
  <p>An error occurred with the following message:</p>
  <blockquote>
  <p>Unable to create an object of type 'MigrationDbContext'. 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>I've been investigating the problem, and I've tried multiple solutions but I wasn't able to solve it yet. So I'm going to share the stack trace of the error hoping to get help from you!</p>
  <p>Stack Trace:</p>
  <blockquote>
  <p>PM&gt; Add-Migration 'Initial' -verbos Using project
  'migration.presentence'. Using startup project 'e-commerce.migration'.
  Build started... Build succeeded. C:\Program Files\dotnet\dotnet.exe
  exec --depsfile
  C:\dev\backlogheros\e-commerce.migration\e-commerce.migration\bin\Debug\netcoreapp5.0\e-commerce.migration.deps.json
  --additionalprobingpath C:\Users\yousi.nuget\packages --runtimeconfig C:\dev\backlogheros\e-commerce.migration\e-commerce.migration\bin\Debug\netcoreapp5.0\e-commerce.migration.runtimeconfig.json
  C:\Users\yousi.nuget\packages\microsoft.entityframeworkcore.tools\5.0.1\tools\netcoreapp2.0\any\ef.dll
  migrations add Initial --json --verbose --no-color --prefix-output
  --assembly C:\dev\backlogheros\e-commerce.migration\e-commerce.migration\bin\Debug\netcoreapp5.0\migration.presentence.dll
  --startup-assembly C:\dev\backlogheros\e-commerce.migration\e-commerce.migration\bin\Debug\netcoreapp5.0\e-commerce.migration.dll
  --project-dir C:\dev\backlogheros\e-commerce.migration\migration.presentence<br />
  --language C# --working-dir C:\dev\backlogheros\e-commerce.migration --root-namespace migration.presentence Using assembly 'migration.presentence'. Using startup assembly
  'e-commerce.migration'. Using application base
  'C:\dev\backlogheros\e-commerce.migration\e-commerce.migration\bin\Debug\netcoreapp5.0'.
  Using working directory
  'C:\dev\backlogheros\e-commerce.migration\e-commerce.migration'. Using
  root namespace 'migration.presentence'. Using project directory
  'C:\dev\backlogheros\e-commerce.migration\migration.presentence'.
  Remaining arguments: . Finding DbContext classes... Finding
  IDesignTimeDbContextFactory implementations... Finding application
  service provider in assembly 'e-commerce.migration'... 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 'MigrationDbContext'.
  Microsoft.EntityFrameworkCore.Design.OperationException: Unable to
  create an object of type 'MigrationDbContext'. 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>  ---&gt;
  System.InvalidOperationException: Unable to resolve service for type
  'Microsoft.EntityFrameworkCore.DbContextOptions' while attempting to
  activate 'migration.presentence.MigrationDbContext'.</p>
  </blockquote>
  <pre><code>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()
  --- End of inner exception stack trace ---    
  at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.&lt;&gt;c__DisplayClass13_4.&lt;FindContextTypes&gt;b__13()
  at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)    
  at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)    
  at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)    
  at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)    
  at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.&lt;&gt;c__DisplayClass0_0.&lt;.ctor&gt;b__0()
  at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.&lt;&gt;c__DisplayClass3_0`1.&lt;Execute&gt;b__0()
  at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  </code></pre>