kosmakoff
kosmakoff

Reputation: 368

How to make use of environment parameter passed to EntityFramework Core CLI

Some of Entity Framework Core CLI commands accept environment parameter. For example dotnet ef migrations list --environment <env_name>.

The assembly where I keep migrations is separate from data entities assembly, therefore I had to derive from IDbContextFactory.

And here comes my question: is there a way to get the environment parameter passed to CLI from inside my DbContextFactory class? I need this to apply specific environment's connections strings.

Thanks in advance.

Upvotes: 1

Views: 1475

Answers (2)

Alan Larimer
Alan Larimer

Reputation: 598

ASP.NET Core and EF Core 2.0.1

In order to grant elevated permissions for migrating entity changes while maintain lower privileges for the application, two SQL users were created.

The connection information for the application is stored in appsettings.json. The connection information for use with the EF CLI is stored in appsettings.<ENVIRONMENT_NAME>.json. Both use the same ConnectionStrings key.

For the dotnet ef to execute successfully, the ASPNETCORE_ENVIRONMENT is set before attempting to update the database.

set ASPNETCORE_ENVIRONMENT=<ENVIRONMENT_NAME>
dotnet ef database update

Upvotes: 1

kosmakoff
kosmakoff

Reputation: 368

Stupid me, after the day of looking around, I noticed that Create method of IDbContextFactory interface has the parameter of type DbContextFactoryOptions, which in turn has EnvironmentName property, which appears to be the one passed to CLI commands.

Case closed.

Upvotes: 5

Related Questions