如何使用实体框架dbmigration更改列到身份? [英] How to alter column to identity using entity framework dbmigration?

查看:116
本文介绍了如何使用实体框架dbmigration更改列到身份?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Entity Framework 5.0和电动工具反向设计了一个小数据库。然后我开始开发一个网站。我遇到一个问题,因为我忘记在我的一个表中的ID列中添加了一个IDENTITY列。



所以我添加了一个代码第一次迁移,并使用更新-database命令在包管理器控制台中。这是我写的Up方法:

  public override void Up()
{
Sql (DELETE FROM dbo.Sections);
Sql(DELETE FROM dbo.QuestionInstances);

DropForeignKey(dbo.SectionInstances,SectionID,dbo.Sections);
DropForeignKey(dbo.QuestionInstances,SectionID,dbo.Sections);

DropTable(dbo.Sections);

CreateTable(
dbo.Sections,
c => new
{
ID = c.Int(nullable:false,identity:true )
UniqueDescription = c.String(nullable:false,maxLength:50),
Heading = c.String(maxLength:500),
})
.PrimaryKey(t = t.ID);

AddForeignKey(dbo.SectionInstances,SectionID,dbo.Sections);
AddForeignKey(dbo.QuestionInstances,SectionID,dbo.Sections);
}

我检查数据库并已成功更改。



我期待实体框架更新其模型并且明确地停止设置ID,但是看起来我错了,因为当我尝试保存我现在得到这个错误:当IDENTITY_INSERT设置为OFF时,无法在表节中插入标识列的显式值



如何让实体框架识别列是现在一个身份列?

解决方案

由于您在Id列尚未设置为IDENTITY时反向设计数据库,因此已经生成了代码第一个模型对于实体的Id属性设置 DatabaseGeneratedOption.None



这将导致EF创建一个insert语句Id设置,将列更改为IDENTITY后不再工作。



您必须手动修复该配置,方法是将其设置为 DatabaseGeneratedOption .Identity 或者完全删除它,因为这是整数字段的默认值。


I reverse engineered a small database using Entity Framework 5.0 and the power tools. Then I started developing a website. I hit a problem because I had forgotten to make the ID column on one of my tables an IDENTITY column.

So I added a code first migration and ran it using the update-database command in the package manager console. Here is the "Up" method I wrote:

    public override void Up()
    {
        Sql("DELETE FROM dbo.Sections");
        Sql("DELETE FROM dbo.QuestionInstances");

        DropForeignKey("dbo.SectionInstances", "SectionID", "dbo.Sections");
        DropForeignKey("dbo.QuestionInstances", "SectionID", "dbo.Sections");

        DropTable("dbo.Sections");

        CreateTable(
            "dbo.Sections",
            c => new
            {
                ID = c.Int(nullable: false, identity: true),
                UniqueDescription = c.String(nullable: false, maxLength: 50),
                Heading = c.String(maxLength: 500),
            })
            .PrimaryKey(t => t.ID);

        AddForeignKey("dbo.SectionInstances", "SectionID", "dbo.Sections");
        AddForeignKey("dbo.QuestionInstances", "SectionID", "dbo.Sections");
    }

I check the database and it has been altered successfully.

I was expecting the entity framework to update its model and stop setting the ID explicitly, but it would appear that I am wrong because when I try to Save I now get this error: "Cannot insert explicit value for identity column in table 'Sections' when IDENTITY_INSERT is set to OFF"

How do I get the entity framework to recognise that the column is now an identity column?

解决方案

Since you reverse-engineered the database when the Id column was not set as IDENTITY yet, the code first model has been generated with DatabaseGeneratedOption.None set for the Id property on the entity.

That causes EF to create an insert statement with the Id set, which no longer works after changing the column to IDENTITY.

You have to manually fix that configuration, by either setting it to DatabaseGeneratedOption.Identity or just removing it altogether, since that is the default value for integer fields.

这篇关于如何使用实体框架dbmigration更改列到身份?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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