title: Entity Framework Core Using multiple DbContexts
body: <p>I'm having a problem that when I try to access a field in my PartsDbContext I get the following error: </p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: 'Invalid object name 'fieldName''</p>
  </blockquote>
  
  <p>It seems that this is because I'm trying to make my PartsDbContext use the same database as my ApplicationDbContext which is used with Identity.  I need to know how to setup a 2nd dbcontext to work with EF core that uses/creates a different database.  </p>
  
  <p>I've tried creating a 2nd connection string but that gets me this error:</p>
  
  <blockquote>
    <p>System.Data.SqlClient.SqlException: 'Cannot open database "PartsDb" requested by the login. The login failed.
    Login failed for user 'DESKTOP-4VPU567\higle'.'</p>
  </blockquote>
  
  <p>Here's my code:</p>
  
  <p><strong>appsettings.json</strong></p>
  
  <pre><code>"ConnectionStrings": {
      "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-PrecisionCustomPC-b14db89e-86ad-4855-a17f-ac64a04339aa;Trusted_Connection=True;MultipleActiveResultSets=true",
      "PartsConnection":  "Server=(localdb)\\mssqllocaldb;Database=PartsDb"
  },
  "Logging": {
      "IncludeScopes": false,
      "LogLevel": {
          "Default": "Warning"
      }
  }
  </code></pre>
  
  <p><strong>PartsDbContext.cs</strong></p>
  
  <pre><code>public class PartsDbContext : DbContext
  {
      public DbSet&lt;PartsViewModels.Tower&gt; Towers { get; set; }
      public DbSet&lt;PartsViewModels.Motherboard&gt; Motherboards { get; set; }
  
      public PartsDbContext(DbContextOptions&lt;PartsDbContext&gt; options)
          : base(options)
      {
      }
  }
  </code></pre>
  
  <p><strong>Startup.cs</strong></p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      // Add framework services.
      services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
           options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
      services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;()
          .AddEntityFrameworkStores&lt;ApplicationDbContext&gt;()
          .AddDefaultTokenProviders();
  
      services.AddEntityFramework()
          .AddDbContext&lt;PartsDbContext&gt;(options =&gt;
            options.UseSqlServer(Configuration.GetConnectionString("PartsConnection")));
  
      services.AddMvc();
  
      services.AddAuthorization(options =&gt;
      {
          options.AddPolicy("RequireAdminRole", policy =&gt; policy.RequireRole("Admin"));
      });
  
      // Add application services.
      services.AddTransient&lt;IEmailSender, AuthMessageSender&gt;();
      services.AddTransient&lt;ISmsSender, AuthMessageSender&gt;();
  }
  </code></pre>
  
  <p><strong>AdminController.cs</strong></p>
  
  <pre><code>[Authorize(Policy = "RequireAdminRole")]
  public class AdminController : Controller
  {
      private readonly PartsDbContext _context;
  
      public AdminController(PartsDbContext context)
      {
          _context = context;
      }
  
      public IActionResult Index()
      {
          return View();
      }
  
      public IActionResult Towers()
      {
          var model = _context.Towers.ToList();
          return View(model);
      }
  }
  </code></pre>
  
  <p>The line <code>var model = _context.Towers.ToList();</code> is where the error is showing up.</p>
  
  <p>Once again. I want to setup my PartsDbContext to work with Entity Framework Core in a way that EF-Core will automatically create the database.</p>