title: .NET Core 2 with EF Core different contexts with different schemas in same DB(SQL Server)
body: <p>I would like to have my tables in a C# ASP.NET Core 2 application in two separate schemas.  I have created two separate DB contexts and different connection strings in <code>appsettings.json</code>. </p>
  
  <p>appsettings.json </p>
  
  <pre><code>"DefaultConnection": "Server=db;Database=DB;User=u;Password=pwd;",
  "InventoryCon": "Server=db;Database=DB;User=u;Password=pwd;"
  </code></pre>
  
  <p>startup:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddDbContext&lt;CustomerDbContext&gt;(options =&gt;
          options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
  
      services.AddDbContext&lt;InventoryDbContext&gt;(options =&gt;
          options.UseSqlServer(Configuration.GetConnectionString("InventoryCon")));
  
      services.AddMvc();
  }
  </code></pre>
  
  <p>I am specifying default schema in each of the dbContextClasses:
  Inventory:</p>
  
  <pre><code>...protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.HasDefaultSchema("inventory");
  }
  </code></pre>
  
  <p>Customer:</p>
  
  <pre><code>...protected override void OnModelCreating(ModelBuilder modelBuilder)
  {
      modelBuilder.HasDefaultSchema("customer");
  }
  </code></pre>
  
  <p>In Program I am trying to seed both:</p>
  
  <pre><code>public static void Main(string[] args)
  {
      var host = BuildWebHost(args);
  
      using (var scope = host.Services.CreateScope())
      {
          var services = scope.ServiceProvider;
          try
          {
              var context = services.GetRequiredService&lt;CustomerDbContext&gt;();
              DbInitializer.Initialize(context);
  
          }
          catch (Exception ex)
          {
              var logger = services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
              logger.LogError(ex, "An error occurred while seeding Customer database.");
          }
  
          try
          {
              var contexti = services.GetRequiredService&lt;InventoryDbContext&gt;();
              DbInitializer.InitInventory(contexti);
          }
          catch (Exception ex)
          {
              var logger = services.GetRequiredService&lt;ILogger&lt;Program&gt;&gt;();
              logger.LogError(ex, "An error occurred while seeding Inventory database.");
          }
      }
  
      host.Run();
  }
  </code></pre>
  
  <p>The above works fine if the connections strings are to different databases so I am guessing it has an issue with two connection strings to the same DB.  Ideally I could separate out these tables into separate schemas so I can better control access for queries.</p>
  
  <p>Any ideas on how I can separate my tables into separate schemas? </p>
  
  <p>EDIT:  Adding ERROR:</p>
  
  <blockquote>
    <p>info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
          Entity Framework Core 2.0.1-rtm-125 initialized 'InventoryDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
    Microsoft.EntityFrameworkCore.Infrastructure:Information: Entity Framework Core 2.0.1-rtm-125 initialized 'InventoryDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
    info: Microsoft.EntityFrameworkCore.Database.Command[20101]
          Executed DbCommand (32ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE') SELECT 1 ELSE SELECT 0
    Microsoft.EntityFrameworkCore.Database.Command:Information: Executed DbCommand (32ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE') SELECT 1 ELSE SELECT 0
    'dotnet.exe"' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.4\System.Diagnostics.StackTrace.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    'dotnet.exe"' (CoreCLR: clrhost): Loaded 'C:\Program Files\dotnet\shared\Microsoft.NETCore.App\2.0.4\System.Reflection.Metadata.dll'. Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
    fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
          Failed executing DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
          SELECT CASE
              WHEN EXISTS (
                  SELECT 1
                  FROM [inventory].[DUA] AS [d])
              THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT)
          END
    System.Data.SqlClient.SqlException (0x80131904): Invalid object name 'inventory.DUA'.</p>
  </blockquote>
  
  <p>I noticed the following in the logs when it creates the schema for customer, which never happens for inventory:
  IF SCHEMA_ID(N'customer') IS NULL EXEC(N'CREATE SCHEMA [customer];');</p>