title: Split EF Core migrations to multiple projects
body: <p>Is it possible to split (not moved to single class library project) migrations to several .NET Core class libraries?</p>
  
  <p>I am following architecture design for splitting application to several independent modules. (<a href="http://docs.simplcommerce.com/en/latest/" rel="nofollow noreferrer">Source</a>)</p>
  
  <p>But I having problems splitting migrations to several projects. Is this supported? </p>
  
  <p>In startup I load dll files in specific folders. In <code>DbContext.OnModelCreating</code> I register all types loaded from loaded dll files and register entities.</p>
  
  <pre><code> protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              List&lt;Type&gt; typeToRegisters = new List&lt;Type&gt;();
              foreach (var module in GlobalConfiguration.Modules)
              {
                  typeToRegisters.AddRange(module.Assembly.DefinedTypes.Select(t =&gt; t.AsType()));
              }
  
              RegisterEntities(modelBuilder, typeToRegisters);
  
              base.OnModelCreating(modelBuilder);
  
              RegisterCustomMappings(modelBuilder, typeToRegisters);
          }
  
          private static void RegisterEntities(ModelBuilder modelBuilder, IEnumerable&lt;Type&gt; typeToRegisters)
          {
              var entityTypes = typeToRegisters.Where(x =&gt; x.GetTypeInfo().IsSubclassOf(typeof(EntityBase)) &amp;&amp; !x.GetTypeInfo().IsAbstract);
              foreach (var type in entityTypes)
              {
                  modelBuilder.Entity(type);
              }
          }
  
          private static void RegisterCustomMappings(ModelBuilder modelBuilder, IEnumerable&lt;Type&gt; typeToRegisters)
          {
              var customModelBuilderTypes = typeToRegisters.Where(x =&gt; typeof(ICustomModelBuilder).IsAssignableFrom(x));
              foreach (var builderType in customModelBuilderTypes)
              {
                  if (builderType != null &amp;&amp; builderType != typeof(ICustomModelBuilder))
                  {
                      var builder = (ICustomModelBuilder)Activator.CreateInstance(builderType);
                      builder.Build(modelBuilder);
                  }
              }
          }
  </code></pre>
  
  <p>But I don't how migrations are executed with <code>context.Database.Migrate();</code>  </p>
  
  <p>Idea is that tables are not created/updated if ASP.NET Core deployment doesn't include specific module. If at any point in the future, customer buys module D, I just send them dll. The next startup <code>context.Database.Migrate();</code> would create all tables and apply migrations for that module.</p>
  
  <p><strong>Additional example:</strong><br>
  Idea is to split application to modules. Let's say module A, B and C. Customer 1 buys module A and B and customer 2 buys module A and C.</p>
  
  <p>There is no point that tables for module C are created for customer 1, and tables for module B for customer 2.</p>
  
  <p>At some point in the future, customer 1 buys module C (they receive dll for module C). Now tables should create and all migrations for module C.</p>
  
  <p>As far as I know migrations could be dedicated to specific project. <a href="https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/projects" rel="nofollow noreferrer">Link</a>. The only problem I see is the <code>ContextModelShapshot</code>.</p>