title: EF Core - Add new tables to database During Runtime
body: <p>I have an asp.net core project which needs to be able to support plugins at runtime, and as a consequence, I need to generate database tables based on what has been plugged in. The plugins are each divided in separate projects and they have have their own DbContext class. The plugins to be used are not known during compile-time, only at runtime.</p>
  
  <p>Now in EF Core I thought that there would be a method like "UpdateDatabase" where you can just add tables to the existing database, but I was wrong. Is there a way to accomplish this? I was able to generate a separate database for each of the plugins, but that wasn't quite what I had in mind..I needed all tables in one database.</p>
  
  <p>Here's the code for the "HRContext" plugin:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using Microsoft.EntityFrameworkCore;
  using Plugins.HR.Models.Entities;
  
  namespace Plugins.HR.Contexts
  {
      public class HrContext : DbContext
      {
          public HrContext()
          {
          }
          public HrContext(DbContextOptions&lt;HrContext&gt; contextOptions) : base(contextOptions)
          {
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.HasDefaultSchema("HR");
              base.OnModelCreating(modelBuilder);
          }
  
          public DbSet&lt;Address&gt; Address { get; set; }
          public DbSet&lt;Attendance&gt; Attendance { get; set; }
          public DbSet&lt;Department&gt; Departments { get; set; }
          public DbSet&lt;Employee&gt; Employees { get; set; }
          public DbSet&lt;JobTitle&gt; JobTitles { get; set; }
      }
  }
  </code></pre>
  
  <p>Here's another piece of code for the "CoreContext" plugin:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Text;
  using Core.Data.Models;
  using Microsoft.EntityFrameworkCore;
  namespace Core.Data.Contexts
  {
      public class CoreContext : DbContext
      {
          public CoreContext()
          {
  
          }
          public CoreContext(DbContextOptions&lt;CoreContext&gt; contextOptions) : base(contextOptions)
          {
  
          }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.HasDefaultSchema("Core");
              base.OnModelCreating(modelBuilder);
  
          }
          public DbSet&lt;Test&gt; Tests { get; set; }
      }
  }
  </code></pre>
  
  <p>My ConfigureServices method in Startup.cs:</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddDbContext&lt;CoreContext&gt;(options =&gt; options.UseSqlServer("Data source = localhost; initial catalog = Company.Core; integrated security = true;"))
      .AddDbContext&lt;HrContext&gt;(options =&gt; options.UseSqlServer("Data source = localhost; initial catalog = Company.HR; integrated security = true;"));
  
      // Add framework services.
      services.AddMvc();
  }
  </code></pre>
  
  <p>If I try to change the connection string to be the same, sooner or later I will get an error that says that the table for one plugin does not exist. I tried "EnsureCreated" but that didn't work too.</p>