I have a DbContext implementation (data repository pattern), in which I change the default schema, as:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("tm");
base.OnModelCreating(modelBuilder);
}
I run the following commands in the Package Manager Console, to generate a .sql script for database creation:
Enable-Migrations -ConnectionStringName "EfDataRepository"
Add-Migration Initial -ConnectionStringName "EfDataRepository"
Update-Database -ConnectionStringName "EfDataRepository" -Script -SourceMigration:0
.. which gives me the following script:
DECLARE @CurrentMigration [nvarchar](max)
IF object_id('[dbo].[__MigrationHistory]') IS NOT NULL
SELECT @CurrentMigration =
(SELECT TOP (1)
[Project1].[MigrationId] AS [MigrationId]
FROM ( SELECT
[Extent1].[MigrationId] AS [MigrationId]
FROM [dbo].[__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = N'xxxxxxxx'
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC)
IF object_id('[tm].[__MigrationHistory]') IS NOT NULL
SELECT @CurrentMigration =
(SELECT TOP (1)
[Project1].[MigrationId] AS [MigrationId]
FROM ( SELECT
[Extent1].[MigrationId] AS [MigrationId]
FROM [tm].[__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = N'xxxxxxxx'
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC)
IF @CurrentMigration IS NULL
SET @CurrentMigration = '0'
IF @CurrentMigration < '201607151403491_Initial'
BEGIN
IF schema_id('tm') IS NULL
EXECUTE('CREATE SCHEMA [tm]')
CREATE TABLE [tm].[Settings] (
[Id] [int] NOT NULL IDENTITY,
[Key] [nvarchar](max) NOT NULL,
[Value] [nvarchar](max),
CONSTRAINT [PK_tm.Settings] PRIMARY KEY ([Id])
)
... and so on
It's all good... EXCEPT, why does it generate a block, which inspects the __MigrationHistory within the dbo schema?! And then it correctly proceeds to inspect the __MigrationHistory from my custom schema (correct behavior).
At the moment, I am just deleting the dbo related block, because I really want to isolate my schema from any interaction with the default one. When I generate a script, I don't want it to do anything outside the 'tm' schema.
Why does EF generate the script in this way? Is there really any reason to query the __MigrationHistory from within the dbo, when the rest of the script never puts anything there? Am I misunderstanding something?