Open Closed

Database seed production only IHostingEnvironment #7144


0
digitalcontrol created

I want to seed database on app start only in production mode. What is the best way to do that? I injected IHostingEnvironment in RIMIKSXEntityFrameworkCoreModule and added code

public override void Initialize() { IocManager.RegisterAssemblyByConvention(typeof(RIMIKSXEntityFrameworkCoreModule).GetAssembly());

// ignore seeding in development
if (_env.IsDevelopment())
{
    SkipDbSeed = true;
}

}

and it works fine. But now Migrator project and Unit tests don't work, because of that dependency. This is the error message: Can't create component 'RIMIKSX.EntityFrameworkCore.RIMIKSXEntityFrameworkCoreModule' as it has dependencies to be satisfied.


3 Answer(s)
  • 0
    maliming created

    You can try use the Substitute class library mock IHostingEnvironment.

    see: https://github.com/aspnetboilerplate/aspnetboilerplate/issues/4186#issuecomment-454731547

  • 0
    digitalcontrol created

    We are still stucked with this, can you give us more detail instructions how to solve this issue? It is very important since we can not have benefits of unit testing.

  • 0
    maliming created

    hi digitalcontrol In fact, you can modify the value of SkipDbSeed directly in Migrator and unit tests.

    Then modify the value of SkipDbSeed according to the environment variable in the web module.

    see: https://github.com/aspnetzero/aspnet-zero-core/blob/dev/aspnet-core/src/MyCompanyName.AbpZeroTemplate.Migrator/AbpZeroTemplateMigratorModule.cs#L20