2013-12-19 71 views
6

我已更新到EF6,但它並沒有樂趣。我創建了一個新的遷移,只是將兩個字段更改爲空。EF 6.0遷移:MigrationHistory中的ContextKey爲空

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)); 
    } 
} 

當我運行update-database我得到以下錯誤:

Cannot insert the value NULL into column 'ContextKey', table 'ScabsContext.dbo.__MigrationHistory'; column does not allow nulls. INSERT fails. The statement has been terminated.

我發現了幾篇文章提到在MigrationHistory新ContextKey領域,但沒有,回答我的問題......這是爲什麼呢字段null?有沒有一種方法(並且我需要)爲ContextKey指定一個值?我認爲這是自動完成的?

回答

13

這對我來說就像您在進行更改時可能已經使用數據庫來測試您的遷移。一個EF5遷移歷史表的結構如下:

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) 
) 

當我從升級至EF5 EF6一個項目,我添加了一個明確的遷移用於此目的。如果您使用的是十進制字段,那麼無論如何都需要進行遷移,因爲它無論如何都使用明確的精度來重新創建這些字段。當您運行EF6下首次遷移,它使用重新創建新的結構,它看起來像遷移歷史表...

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]) 
) 

你可以看到,該表包括ContextKey場是不爲空。由於您遇到了錯誤,我建議您嘗試在EF6格式的數據庫上使用EF5進行遷移。

如果要恢復你的數據庫的EF5格式,以便運行從EF5遷移,只是下降的ContextKey場並重新創建主鍵:

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) 
+0

恩,說,謝謝。 – tkt986

4

我創建了下面的腳本來更新ef5 MigrationHistory到ef6。您可能需要將Migrations.Configuration更改爲您的名字空間。

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 
+0

爲了使INSERT INTO語句正常工作,我不得不將SQL Azure DB升級到V12,但之後它運行良好,謝謝! –