abp icon indicating copy to clipboard operation
abp copied to clipboard

Environment on IDataSeedContributor?

Open aklscv opened this issue 6 years ago • 3 comments

Is it possible to get the environment in a IDataSeedContributor? I want to be able to decide about seeding data or not or even the data to be seeded based on the environment.

It seems that I can't inject IHostingEnvironment from the DbMigrator. I created a solution to check the environment on the ConfigureServices and use different DataSeeders but it doesn't seem a nice solution so I was wondering if you have a better idea.

Thanks!

aklscv avatar Apr 14 '20 07:04 aklscv

It seems that I can't inject IHostingEnvironment from the DbMigrator.

You can refer to this issue: https://github.com/abpframework/abp/issues/3429

DataSeedContext can pass parameters.

await DataSeeder.SeedAsync(
				new DataSeedContext(tenant.Id)
					.WithProperty("AdminEmail", input.AdminEmailAddress)
					.WithProperty("AdminPassword", input.AdminPassword)
				);

maliming avatar Apr 14 '20 08:04 maliming

Thanks for replying. The issue that you mentioned doesn't say anything about passing the current environment to seeders unless I am missing something. I've already got the code down to load different environments on the DbMigrator, my problem is injecting this along to other classes like DataSeeders.

I also don't think passing parameters to the DataSeedContext will help because, in the migration context, it resides on the DbMigrationService which I will not be able to inject a IHostingEnvironment too since it apparently it doesn't exist in Console Apps.

aklscv avatar Apr 14 '20 14:04 aklscv

I think what mailming is saying is that you can add new DataSeedContext().WithProperty("Environment", "Production")); to here: https://github.com/abpframework/abp/blob/523bc071ad63104683e80e76aef0e68d6c90f101/templates/app/aspnet-core/src/MyCompanyName.MyProjectName.Domain/Data/MyProjectNameDbMigrationService.cs#L68

Alternatively I think you could also use Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") anywhere you need it. Just pass "ASPNETCORE_ENVIRONMENT" as an argument when you start the console app.

You can also add IConfiguration to your DbMigrationService/IDataSeedContributor constructors to access your config via dependency injection.

olicooper avatar Apr 16 '20 16:04 olicooper