Question

I'm start developing modular system with asp.net core. the system has module loader, module installer and so on. each module has it's own database model that dynamically add to Context

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
      var typeToRegisters = new List<Type>();
      foreach (var module in GlobalConfiguration.Modules)
      {
          typeToRegisters.AddRange(module.Assembly.DefinedTypes.Select(t => t.AsType()));
      }

      RegisterEntities(modelBuilder, typeToRegisters);
      base.OnModelCreating(modelBuilder);
}

private static void RegisterEntities(ModelBuilder modelBuilder, IEnumerable<Type> typeToRegisters)
{
      var entityTypes = typeToRegisters.Where(x => x.GetTypeInfo().IsSubclassOf(typeof(IEntity<>)) && !x.GetTypeInfo().IsAbstract);
      foreach (var type in entityTypes)
      {
          modelBuilder.Entity(type);
      }
}

But EF Core doesn't work without migration. i can create all the migrations file for all modules and run it in start, but it's wrong. maybe a module doesn't need to be install at all.

is there any way that each module bring it's own migration? or any way to solve this?

Sorry for my bad English

Thanks

1
2
5/15/2017 5:26:54 AM

Accepted Answer

But EF Core doesn't work without migration.

It works! EF Core isn't like EF 6.x and it doesn’t have automatic migrations concept. So unless you ask for applying pending migrations using context.Database.Migrate() method, it won't check the migrations history table at all. So EF Core doesn't need an explicit migration. To test it, just run your application and then you will see it will complain about missing tables and fields, related to your actual executed queries. It doesn't complain about missing migrations history at all. In this case you can create your database tables separately, without using the migrations and it will work just fine. So to work independently form the migrations (this is your question), create an interface like

public interface  IModuleDatabaseScript
{
   string CreateSqlSchema { get;}
}

And then your modules should implement this interface and provide the create database/create table/alter table/drop table, etc. Then your context should read these scripts and then run them using context.Database.ExecuteSqlCommand method. You should do this when a module is being loaded to memory once. In this case each module can have or not (just return empty CreateSqlSchema string) database creation strategy, without using the EF Core's migration mechanism.

6
5/15/2017 9:27:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow