EF 6.0迁移:MigrationHistory中的ContextKey为null [英] EF 6.0 Migrations: ContextKey in MigrationHistory is null

查看:48
本文介绍了EF 6.0迁移:MigrationHistory中的ContextKey为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已更新到EF6,但它并不有趣。我创建了一个新的迁移,该迁移只是将两个字段更改为可为空。

I've updated to EF6 and it hasn't been fun. I have created a new migration that is just changing 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));
    }
}

当我运行 update-数据库我收到以下错误:

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


无法将值NULL插入表'ScabsContext的列'ContextKey'中.dbo .__ MigrationHistory';列不允许为空。 INSERT失败。
语句已终止。

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字段,但没有其他内容回答我的问题...为什么此字段为空?有没有一种方法(我需要)为 ContextKey 指定一个值?我以为是自动完成的?

I've found a few articles mentioning the new ContextKey field in MigrationHistory but nothing that answers my questions... Why is this field null? Is there a way (and do I need to) specify a value for ContextKey? I thought that was done automatically?

推荐答案

在我看来,您好像一直在使用数据库来测试迁移情况正在进行更改。 EF5迁移历史记录表具有以下结构:

This looks to me like you may have been using a database for testing your migrations when you were making the change. An 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 )
)

将项目从EF5升级到EF6,为此我添加了显式迁移。如果您使用的是十进制字段,则无论如何都需要进行迁移,因为无论如何它都会使用显式精度重新创建它们。当您在EF6下运行首次迁移时,它将使用新结构重新创建迁移历史记录表,如下所示:

When I upgraded a project from EF5 to EF6, I added an explicit migration for this purpose. If you're using decimal fields, then a migration would be necessary anyway, as it recreates these using an explicit precision anyway. When you run this first migration under EF6, it recreates the migration history table using the new structure, which 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])
)

您可以看到此表包含一个ContextKey字段,该字段不为空。由于出现错误,建议您尝试在EF6格式的数据库上使用EF5进行迁移。

You can see that this table includes a ContextKey field which is not null. Because of the error you're getting, I would suggest that you're trying to run a migration using EF5 on an EF6-format database.

如果要还原您的数据库为EF5格式,以便从EF5运行迁移,只需删除ContextKey字段并重新创建主键:

If you want to revert your database to the EF5 format in order to run a migration from EF5, just drop 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)

这篇关于EF 6.0迁移:MigrationHistory中的ContextKey为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆