title: Unable to create an object of type 'DbContext'
body: <p>When I try to run</p>
  
  <pre><code>dotnet ef migration add Init
  </code></pre>
  
  <p>I get error</p>
  
  <blockquote>
    <p>Unable to create an object of type 'IdentityContext'. </p>
  </blockquote>
  
  <p>I know what caused the problem. I wanted to learn using message bus and added it to my project, so I run for some course and implemented it. Ofc bus working perfectly. But the problem is that I can no longer migrate via EF.</p>
  
  <p>My Startup.cs before was.</p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          CreateWebHostBuilder(args).Build().Run();
      }
  
      public static IWebHostBuilder CreateWebHostBuilder(string[] args) =&gt;
          WebHost.CreateDefaultBuilder(args)
              .UseStartup&lt;Startup&gt;();
  }
  </code></pre>
  
  <p>Now it looks like this.</p>
  
  <pre><code>public static void Main(string[] args)
  {
    ServiceHost.Create&lt;Startup&gt;(args)
      .UseRabbitMq()
      .Build()
      .Run();
  }
  </code></pre>
  
  <p>And ServiceHost class</p>
  
  <pre><code>public void Run() =&gt; _webHost.Run();
  
  public static HostBuilder Create&lt;TStartup&gt;(string[] args) where TStartup : class
  {
    Console.Title = typeof(TStartup).Namespace;
    var config = new ConfigurationBuilder()
      .AddEnvironmentVariables()
      .AddCommandLine(args)
      .Build();
    var webHostBuilder = WebHost.CreateDefaultBuilder()
      .UseConfiguration(config)
      .UseStartup&lt;TStartup&gt;();
  
    return new HostBuilder(webHostBuilder.Build());
  }
  </code></pre>
  
  <p>So anyone would give advice why migration stopped to work? For me it looks like it should work, but it isn't so I guess I'm wrong.</p>
  
  <p>And ofc I have in my Startup.cs</p>
  
  <pre><code>services.AddEntityFrameworkNpgsql().AddDbContext&lt;IdentityContext&gt;(options =&gt;
      options.UseNpgsql(Configuration.GetConnectionString("IdentityConnection")));
  </code></pre>
  
  <p>EDIT
  IdentityContext class:</p>
  
  <pre><code>  public class IdentityContext : DbContext
    {
      public IdentityContext(DbContextOptions&lt;IdentityContext&gt; options) : base(options)
      {
        while (!Debugger.IsAttached)
        {
          Thread.Sleep(100);
        }
      }
      public DbSet&lt;User&gt; Users { get; set; }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
        modelBuilder.Entity&lt;User&gt;().HasIndex(user =&gt; user.Email).IsUnique();
        modelBuilder.Entity&lt;User&gt;().HasIndex(user =&gt; user.Username).IsUnique();
      }
    }
  </code></pre>
  
  <p>EDIT2.
  Verbose migration</p>
  
  <pre><code>Using project '/home/msek/Projects/inz/CrossX/src/CrossX.Identity/CrossX.Identity.csproj'.
  Using startup project '/home/msek/Projects/inz/CrossX/src/CrossX.Identity/CrossX.Identity.csproj'.
  Writing '/home/msek/Projects/inz/CrossX/src/CrossX.Identity/obj/CrossX.Identity.csproj.EntityFrameworkCore.targets'...
  dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpCq3PQa.tmp /verbosity:quiet /nologo /home/msek/Projects/inz/CrossX/src/CrossX.Identity/CrossX.Identity.csproj
  Writing '/home/msek/Projects/inz/CrossX/src/CrossX.Identity/obj/CrossX.Identity.csproj.EntityFrameworkCore.targets'...
  dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=/tmp/tmpR48yu8.tmp /verbosity:quiet /nologo /home/msek/Projects/inz/CrossX/src/CrossX.Identity/CrossX.Identity.csproj
  dotnet build /home/msek/Projects/inz/CrossX/src/CrossX.Identity/CrossX.Identity.csproj /verbosity:quiet /nologo
  
  Build succeeded.
      0 Warning(s)
      0 Error(s)
  
  Time Elapsed 00:00:01.17
  dotnet exec --depsfile /home/msek/Projects/inz/CrossX/src/CrossX.Identity/bin/Debug/netcoreapp2.2/CrossX.Identity.deps.json --additionalprobingpath /home/msek/.nuget/packages --runtimeconfig /home/msek/Projects/inz/CrossX/src/CrossX.Identity/bin/Debug/netcoreapp2.2/CrossX.Identity.runtimeconfig.json /home/msek/.dotnet/tools/.store/dotnet-ef/2.2.2/dotnet-ef/2.2.2/tools/netcoreapp2.2/any/tools/netcoreapp2.0/any/ef.dll migrations add Init --assembly /home/msek/Projects/inz/CrossX/src/CrossX.Identity/bin/Debug/netcoreapp2.2/CrossX.Identity.dll --startup-assembly /home/msek/Projects/inz/CrossX/src/CrossX.Identity/bin/Debug/netcoreapp2.2/CrossX.Identity.dll --project-dir /home/msek/Projects/inz/CrossX/src/CrossX.Identity/ --language C# --working-dir /home/msek/Projects/inz/CrossX/src/CrossX.Identity --verbose --root-namespace CrossX.Identity
  Using assembly 'CrossX.Identity'.
  Using startup assembly 'CrossX.Identity'.
  Using application base '/home/msek/Projects/inz/CrossX/src/CrossX.Identity/bin/Debug/netcoreapp2.2'.
  Using working directory '/home/msek/Projects/inz/CrossX/src/CrossX.Identity'.
  Using root namespace 'CrossX.Identity'.
  Using project directory '/home/msek/Projects/inz/CrossX/src/CrossX.Identity/'.
  Finding DbContext classes...
  Finding IDesignTimeDbContextFactory implementations...
  Finding application service provider...
  Finding IWebHost accessor...
  No CreateWebHostBuilder(string[]) method was found on type 'CrossX.Identity.Program'.
  No application service provider was found.
  Finding DbContext classes in the project...
  Found DbContext 'IdentityContext'.
  Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'IdentityContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---&gt; System.MissingMethodException: No parameterless constructor defined for this object.
     at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean&amp; canBeCached, RuntimeMethodHandleInternal&amp; ctor)
     at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache)
     at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.&lt;&gt;c__DisplayClass12_3.&lt;FindContextTypes&gt;b__13()
     --- End of inner exception stack trace ---
     at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.&lt;&gt;c__DisplayClass12_3.&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)
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType)
     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)
  Unable to create an object of type 'IdentityContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728
  </code></pre>