There's some issue with Entity Framework that doesn't call the 'Seed' method during a migration. With a considerable amount of effort, I've got it down to when it calls HistoryRepositor.GetLastModel (https://github.com/aspnet/EntityFramework6/blob/master/src/EntityFramework/Migrations/History/HistoryRepository.cs). In particular this section;
var baseQuery
= CreateHistoryQuery(context, contextKey)
.OrderByDescending(h => h.MigrationId);
var lastModel
= baseQuery
.Select(
s => new
{
s.MigrationId,
s.Model,
s.ProductVersion
})
.FirstOrDefault();
'baseQuery' has a value as below.
{SELECT
[Project1].[MigrationId] AS [MigrationId],
[Project1].[ContextKey] AS [ContextKey],
[Project1].[Model] AS [Model],
[Project1].[ProductVersion] AS [ProductVersion]
FROM ( SELECT
[Extent1].[MigrationId] AS [MigrationId],
[Extent1].[ContextKey] AS [ContextKey],
[Extent1].[Model] AS [Model],
[Extent1].[ProductVersion] AS [ProductVersion]
FROM [__MigrationHistory] AS [Extent1]
WHERE [Extent1].[ContextKey] = @p__linq__0
) AS [Project1]
ORDER BY [Project1].[MigrationId] DESC}
There are two records in the MigrationHistory table, which seems to be confirmed by using 'baseQuery.ToList()' in the immediate window and is in the correct order (MigrationId descending - these are timestamps so should be newest records first)
baseQuery.ToList()[0];
{System.Data.Entity.Migrations.History.HistoryRow}
ContextKey: "DT.DataAccess.AppContext"
MigrationId: "201612011150460_AutomaticMigration"
Model: {byte[5615]}
ProductVersion: "6.1.0-alpha1"
baseQuery.ToList()[1];
{System.Data.Entity.Migrations.History.HistoryRow}
ContextKey: "DT.DataAccess.AppContext"
MigrationId: "201611291632453_AutomaticMigration"
Model: {byte[5592]}
ProductVersion: "6.1.3-40302"
However, the record assigned to lastModel is not '201612011150460_AutomaticMigration' but rather '201611291632453_AutomaticMigration' which is unexpected.
lastModel = { MigrationId = "201611291632453_AutomaticMigration", Model = {byte[5592]}, ProductVersion = "6.1.3-40302" }
Using baseQuery.FirstOrDefault();
(or First()
) from the Immediate window;
baseQuery.FirstOrDefault();
{System.Data.Entity.Migrations.History.HistoryRow}
ContextKey: "DT.DataAccess.AppContext"
MigrationId: "201611291632453_AutomaticMigration"
Model: {byte[5592]}
ProductVersion: "6.1.3-40302"
As baseQuery
is created with OrderByDescending
this should mean it's in a fixed order, correct?