title: Configuring DbContext Constructor
body: <p>I'm trying to use EF Core tools to manage an SqlServer database I'm designing in a C# class library. It's in a class library because I need to use the database schema in both an MVC6 website and some command line tools.</p>
  
  <p>I had to convert the class library to being a netapp because the current version of the tooling doesn't support class libraries, but I don't think that's the source of my problem.</p>
  
  <p>My DbContext class looks like this:</p>
  
  <pre><code>public class ConnellDbContext : IdentityDbContext&lt;ConnellUser&gt;
  {
      public ConnellDbContext( DbContextOptions&lt;ConnellDbContext&gt; options )
      {
      }
  
      // core tables
      public DbSet&lt;Ballot&gt; Ballots { get; set; }
      public DbSet&lt;Campaign&gt; Campaigns { get; set; }
      //...
  }
  </code></pre>
  
  <p>When I run "dotnet ef migrations list" on the Package Manager Console, I get the following error message:</p>
  
  <blockquote>
    <p>No parameterless constructor was found on 'ConnellDbContext'. Either
    add a parameterless constructor to 'ConnellDbContext' or add an
    implementation of  'IDbContextFactory' in the same
    assembly as 'ConnellDbContext'.</p>
  </blockquote>
  
  <p>I'm not quite sure how to resolve this. It's easy enough to insert a parameterless constructor, but when I do I get the following error:</p>
  
  <blockquote>
    <p>No database provider has been configured for this DbContext. A
    provider can be configured by overriding the DbContext.OnConfiguring
    method or by using AddDbContext on the application service provider.
    If AddDbContext is used, then also ensure that your DbContext type
    accepts a DbContextOptions object in its constructor and
    passes it to the base constructor for DbContext.</p>
  </blockquote>
  
  <p>I >>think&lt;&lt; this means the console commands are not picking up the connection string information in my appsettings.json file:</p>
  
  <pre><code>{
    "ConnectionStrings": {
      "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-ConnellCampaigns;Trusted_Connection=True;MultipleActiveResultSets=true;AttachDbFilename=e:\\SqlServer\\Data\\ConnellCampaigns.mdf;"
    }
  }
  </code></pre>
  
  <p>I'm missing something about how the EF tooling accesses the source code to do its magic. Any pointers or leads would be much appreciated.</p>
  
  <p><strong>Additional Info</strong></p>
  
  <p>Thanx to Mr. Anderson I've made a bit of progress. I added a parameterless constructor and overrode the OnConfiguring() method in my DbContext class:</p>
  
  <pre><code>protected override void OnConfiguring( DbContextOptionsBuilder optionsBuilder )
  {
      var builder = new ConfigurationBuilder()
          .AddJsonFile( "appsettings.json", optional: true, reloadOnChange: true );
  
      IConfigurationRoot config = builder.Build();
  
      optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection") );
  }
  </code></pre>
  
  <p>That didn't work, but explicitly including the actual connection string in the call to UseSqlServer() did. Thoughts on why the call based on "DefaultConnection" didn't work?</p>