Use environment variables with Entity Framework Core Migrations

ef-migrations entity-framework-core sql-server

Question

We are using a different database schema name for each environment (e.g. "dev", "test", "prod"), and I'm trying to setup Migrations with Entity Framework Core.

The first thing I did was Add-Migration Init, which as expected created a Migration for the initial schema. However, this Migration hard-codes the schema name:

migrationBuilder.EnsureSchema(
    name: "dev");

migrationBuilder.CreateTable(
    name: "MyTable",
    schema: "dev",
    columns: table => new
    ...

I guess I could modify the Migration files generated by dotnet ef to use a schema name based on an environment variable, but I'd rather not modify these auto-generated files if possible.

Is there any other way to use an environment-specific schema name?

1
0
2/26/2018 11:45:48 AM

Popular Answer

MS SQL Server supports default_schema for user.

You may create different users with different default schemes (also you may restrict this users from accessing "other" schemes) and use different connection strings for different environments.

0
2/27/2018 3:51:37 PM


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