Skip to content

Migrations ignore default schema #9864

Closed as not planned
Closed as not planned

Description

For testing purposes I try to apply a set of migrations to a different schema for each test run (or in production for a new installation into a customer-supplied schema, for that matter). For that purposes I have a _schema field in my database context and use it to set the default schema on the ModelBuilder in OnModelCreating(). In my migrations all tables are in the standard schema, i.e. don't have a schema-field set. When applying the migrations via ctx.Database.Migrate() (ctx being the context instance with the default schema set), tables are still created in the dbo.-schema on MSSQL. Subsequent access to the tables obviously fails, as the context is looking for the tables in the configured default schema.

When I create a new migration from a context with the default schema set, each table receives the correct schema field, so Add-Migration seems to obey the schema. ctx.Database.Migrate() should do so as well for consistency reasons (at least for tables, that have no explicit schema set).

Further technical details

EF Core version: 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017

Activity

  1. added this to the 2.1.0 milestone on Sep 25, 2017
  2. bricelam commented on Sep 25, 2017

    @bricelam
    Contributor

    Migrations without a schema specified will generate SQL without a schema specified. When executing, it will use the database user's default schema.

    You can change the user's default schema:

    ALTER USER MyUser WITH DEFAULT_SCHEMA = MySchema

    You could also update you migrations to use a custom base type to flow the default schema in:

    class MyMigrationBase : Migration
    {
        public static string DefaultSchema { get; set; }
    }
    class MyMigration1 : MyMigrationBase
    {
        protected override void Up(MigraitonBuilder builder)
        {
            builder.DropColumn(
                schema: DefaultSchema,
                table: "MyTable",
                column: "MyColumn");
        }
    }

    ...then set it before migrating.

    MyMigrationBase.DefaultSchema = ctx.Model.Relational().DefaultSchema;
    ctx.Database.Migrate();
  3. removed this from the 2.1.0 milestone on Sep 25, 2017
  4. ajcvickers commented on Sep 27, 2017

    @ajcvickers
    Contributor

    @markusbrueckner You could look into providing an override of the ModelCacheKeyFactory and registering it with ReplaceService in DbContextOptionsBuilder. Beyond this, we are leaving this issue open to consider some other things we might do with schemas that could be better for this kind of scenario. Will discuss with @divega when he is back.

  5. ajcvickers commented on Oct 4, 2017

    @ajcvickers
    Contributor

    Filed #9965 for some general ideas of improving in this area.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions