title: Entity Framework Core - Migration - No Parameterless Constructor Defined for this Object
body: <p>I am working with the latest .Net Core and EF Core in Visual Studio 2017.  I have created a model and it was working great.  I have since made some modifications and am getting the following error when I try to add a new migration:</p>
  
  <pre><code>Build succeeded.
    0 Warning(s)
    0 Error(s)
  
  Time Elapsed 00:00:09.08
  System.MissingMethodException: No parameterless constructor defined for this object.
    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean&amp; canBeCached, RuntimeMethodHandleInternal&amp; ctor, Boolean&amp; bNeedSecurityCheck)
    at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark&amp; stackMark)
    at System.Activator.CreateInstance(Type type, Boolean nonPublic)
    at System.Activator.CreateInstance(Type type)
    at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.&lt;&gt;c__DisplayClass12_0.&lt;FindContextTypes&gt;b__3()
    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)
  No parameterless constructor defined for this object.
  </code></pre>
  
  <p>I have compared my code since the last check in and have commented out certain chunks of code to see if the error persists and whatever I comment out, it still fails with the same error.</p>
  
  <p><strong>Question:</strong> Is there a way to get more detailed information on exactly WHAT type does not have a parameterless constructor?  Or even run this from within VS and perhaps get a breakpoint?</p>
  
  <p><strong>Update:</strong> Based on some of the comments, here is some code.</p>
  
  <p>For the <code>DbContext</code> override</p>
  
  <pre><code>public class AlmanacDb : IdentityDbContext&lt;ApplicationUser, ApplicationRole, int&gt; {
  
    private readonly ILogger logger;
  
    public AlmanacDb(DbContextOptions&lt;AlmanacDb&gt; options, ILoggerFactory loggerFactory) : base(options) {
      this.logger = loggerFactory.CreateLogger&lt;AlmanacDb&gt;();
    }
  
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
      optionsBuilder.UseSqlServer("Server=.\\SQLExpress;Database=Almanac;Trusted_Connection=True;MultipleActiveResultSets=True;");
    }
  
    ...
  }
  </code></pre>
  
  <p>I do have a <code>IDbContextFactory</code> but it is not referenced anywhere and worked without a reference.  Not sure if this is a problem or not based on a link provided in the 2nd comment.  If memory serves, as long as the <code>IDbContextFactory</code> is within the solution, it should find it?</p>
  
  <pre><code>public class AlmanacDbFactory : IDbContextFactory&lt;AlmanacDb&gt; {
  
    private IConfigurationRoot configuration;
    private readonly ILoggerFactory loggerFactory;
  
    public AlmanacDbFactory(ILoggerFactory loggerFactory) {
      var builder = new ConfigurationBuilder()
       .SetBasePath(System.AppContext.BaseDirectory)
       .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
  
      this.configuration = builder.Build();
      this.loggerFactory = loggerFactory;
    }
  
    public AlmanacDb Create(DbContextFactoryOptions options) {
      var optionsBuilder = new DbContextOptionsBuilder&lt;AlmanacDb&gt;();
      optionsBuilder.UseSqlServer(
        configuration.GetConnectionString("AlmanacSQL"), m =&gt; { m.EnableRetryOnFailure(); }
      );
  
      return new AlmanacDb(optionsBuilder.Options, loggerFactory);
    }
  }
  </code></pre>
  
  <p>My <code>Startup.cs</code> class</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services) {
    try {
      services.AddDbContext&lt;AlmanacDb&gt;(options =&gt;
        options.UseSqlServer(Configuration.GetConnectionString("AlmanacSQL"))
      );
      services.AddScoped(typeof(IRepository&lt;&gt;), typeof(Repository&lt;&gt;));
  
      services.AddIdentity&lt;ApplicationUser, ApplicationRole&gt;()
        .AddEntityFrameworkStores&lt;AlmanacDb, int&gt;()
        .AddDefaultTokenProviders();
  
      services.AddScoped&lt;SignInManager&lt;ApplicationUser&gt;, AvantiaSignInManager&lt;ApplicationUser&gt;&gt;();
  
      services.AddAuthorization(x =&gt; {
        x.AddPolicy("EmployeeOnly", p =&gt; p.RequireClaim("EmployeeNumber"));
      });
  
      services.AddMvc();
    } catch (Exception e) {
      Console.WriteLine(e.ToString());
      throw;
    }
  }
  
  public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, AlmanacDb context) {
    ... // Nothing actually touches the AlmanacDb within this code so I left it out
  }
  </code></pre>
  
  <p><strong>Update 2: Solutiuon</strong>
  Seeing that I am learning ASP.Net Core I am reading a ton of sites (including docs.microsoft.com) at one point I put in the <code>IDbContextFactory</code> code as you can see above.  I removed that from my code and the error went away and everything built and created the migration.</p>
  
  <p>I am going to mark @alessalessio as the answer as I assume (have not tested it yet) that taking out the <code>ILoggerFactory loggerFactory</code> dependency within the <code>AlmanacDbFactory</code> constructor will do the trick as well.</p>