You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
@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.
Activity
bricelam commentedon Sep 25, 2017
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:
You could also update you migrations to use a custom base type to flow the default schema in:
...then set it before migrating.
ajcvickers commentedon Sep 27, 2017
@markusbrueckner You could look into providing an override of the
ModelCacheKeyFactoryand registering it withReplaceServicein 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.ajcvickers commentedon Oct 4, 2017
Filed #9965 for some general ideas of improving in this area.