Base solution for your next web application
Open Closed

Database seed production only IHostingEnvironment #7144


User avatar
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)
  • User Avatar
    0
    maliming created
    Support Team

    You can try use the Substitute class library mock IHostingEnvironment.

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

  • User Avatar
    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.

  • User Avatar
    0
    maliming created
    Support Team

    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