EF 6.0 Migrations: ContextKey in MigrationHistory is null

I upgraded to EF6 and it was not fun. I created a new migration that just changes two fields to nullable.

public partial class AllowNullableFieldsForImage : DbMigration { public override void Up() { AlterColumn("dbo.Scabs", "image_height", c => c.Int()); AlterColumn("dbo.Scabs", "image_width", c => c.Int()); } public override void Down() { AlterColumn("dbo.Scabs", "image_width", c => c.Int(nullable: false)); AlterColumn("dbo.Scabs", "image_height", c => c.Int(nullable: false)); } } 

When I run update-database , I get the following error:

Cannot insert a NULL value in the "ContextKey" column, table "ScabsContext.dbo .__ MigrationHistory"; column does not allow zeros. INSERT fails. Application completed.

I found several articles that mention the new ContextKey field in the MigrationHistory, but nothing answers my questions ... Why is this field empty? Is there a way (and I need) to specify a value for ContextKey ? I thought it was done automatically?

+6
source share
2 answers

This is similar to the fact that you may have used the database to test your migrations when making changes. The EF5 migration history table has the following structure:

 CREATE TABLE [dbo].[__MigrationHistory]( [MigrationId] [nvarchar](255) NOT NULL, [Model] [varbinary](max) NOT NULL, [ProductVersion] [nvarchar](32) NOT NULL CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY CLUSTERED ( [MigrationId] ASC ) ) 

When I upgraded the project from EF5 to EF6, I added an explicit migration for this. If you use decimal fields, then migration will be necessary anyway, since it recreates them using explicit precision anyway. When you start this first migration under EF6, it recreates the migration history table using a new structure that looks like ...

 CREATE TABLE [dbo].[__MigrationHistory2] ( [MigrationId] [nvarchar](150) NOT NULL, [ContextKey] [nvarchar](300) NOT NULL, [Model] [varbinary](max) NOT NULL, [ProductVersion] [nvarchar](32) NOT NULL, CONSTRAINT [PK_dbo.__MigrationHistory2] PRIMARY KEY ([MigrationId], [ContextKey]) ) 

You can see that this table contains a ContextKey field that is not null. Due to the error you are getting, I would suggest that you are trying to migrate using EF5 in an EF6 format database.

If you want to return your database to EF5 format to migrate from EF5, just release the ContextKey field and recreate the primary key:

 ALTER TABLE dbo.__MigrationHistory DROP CONSTRAINT [PK_dbo.__MigrationHistory2] ALTER TABLE dbo.__MigrationHistory DROP COLUMN ContextKey ALTER TABLE dbo.__MigrationHistory ADD CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY (MigrationId) 
+13
source

I created the following script to upgrade ef5 MigrationHistory to ef6. You might want to change Migrations.Configuration to fit your namespace.

 BEGIN TRANSACTION SELECT * INTO [tmp__MigrationHistory] FROM [__MigrationHistory] SELECT * FROM [tmp__MigrationHistory] DROP TABLE [__MigrationHistory] CREATE TABLE [dbo].[__MigrationHistory] ( [MigrationId] [nvarchar](150) NOT NULL ,[ContextKey] [nvarchar](300) NOT NULL ,[Model] [varbinary](max) NOT NULL ,[ProductVersion] [nvarchar](32) NOT NULL ,CONSTRAINT [PK_dbo.__MigrationHistory] PRIMARY KEY CLUSTERED ( [MigrationId] ASC ,[ContextKey] ASC ) WITH ( PAD_INDEX = OFF ,STATISTICS_NORECOMPUTE = OFF ,IGNORE_DUP_KEY = OFF ,ALLOW_ROW_LOCKS = ON ,ALLOW_PAGE_LOCKS = ON ) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO INSERT INTO [__MigrationHistory] ( [MigrationId] ,[ContextKey] ,[Model] ,[ProductVersion] ) SELECT [MigrationId] ,'Migrations.Configuration' ,[Model] ,[ProductVersion] FROM [tmp__MigrationHistory] SELECT * FROM [__MigrationHistory] DROP TABLE [tmp__MigrationHistory] ROLLBACK TRANSACTION 
+4
source

All Articles