We have a project that contains multiple EntityFramework contexts, each with migrations enabled. Now when we create a full migration script using Update-Database -SourceMigration $initialDatabase -script
we get a script per context.
This script however starts with something like this:
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'Some.Configuration.Here'
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC)
IF @CurrentMigration IS NULL
SET @CurrentMigration = '0'
IF @CurrentMigration < '201506111916518_InitialCreate'
BEGIN
...
<migration code here>
...
END
Note how it gets the current migration using the ContextKey, and if it doesn't find it, it'll do the first migration.
Things get hairy because the first migration contains the code to create the __MigrationHistory
table, which obviously already exists when you've already run the first script.
Is this some edge-case bug or am I doing something wrong here?
Thanks in advance