您可以使用Entity Framework 6从主键中删除身份吗? [英] Can you remove Identity from a primary key with Entity Framework 6?

查看:92
本文介绍了您可以使用Entity Framework 6从主键中删除身份吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先通过实体框架代码创建了一个表,并将主键设置为自动递增,但是现在我想从列中删除该自动递增.我尝试使用两个流利的API来做到这一点:

I created a table via entity framework code-first with a primary key set to auto increment, but now I want to remove that auto-incrementing from the column. I've tried doing that with both fluent API:

public class ProductTypeMap: EntityTypeConfiguration<ProductType>
{
    public ProductTypeMap()
    {
        // This is an enum effectively, so we need fixed IDs
        Property(x => x.ProductTypeId)
            .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
    }

}

和注释:

public class ProductType
{
    [Required, Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public int ProductTypeId { get; set; }
    public string ProductTypeName { get; set; }

}

在这两种情况下,它们都产生相同的迁移代码:

And in both cases they produce the same migration code:

public partial class removeproducttypeidentity : DbMigration
{
    public override void Up()
    {
        DropPrimaryKey("dbo.ProductTypes");
        AlterColumn("dbo.ProductTypes", "ProductTypeId", c => c.Int(nullable: false));
        AddPrimaryKey("dbo.ProductTypes", "ProductTypeId");
    }

    public override void Down()
    {
        DropPrimaryKey("dbo.ProductTypes");
        AlterColumn("dbo.ProductTypes", "ProductTypeId", c => c.Int(nullable: false, identity: true));
        AddPrimaryKey("dbo.ProductTypes", "ProductTypeId");
    }
}

但是,当我在数据库上运行该迁移时,是否从SQL Server 2008数据库表中删除了身份规范?

However, when I run that migration on the database, the Identity Specification is not removed from the SQL Server 2008 database table?

我还尝试了如下所示在迁移中显式关闭身份,但是也没有这样做:

I also tried explicitly turning off the Identity in the migration as follows, but that didn't do it either:

    AlterColumn("dbo.ProductTypes", "ProductTypeId", c => c.Int(nullable: false, identity: false));

还有另一种方法告诉SQL删除身份吗?

Is there another way to tell SQL to remove the Identity?

推荐答案

您不能使用ALTER COLUMN设置列是否为标识列(

You can not use ALTER COLUMN to set whether a column is an identity column (How to alter column to identity(1,1)).

相反,您必须:

  • (备份数据库)
  • CREATE TMP_table具有原始表的列,但ID列设置为标识:false
  • SET IDENTITY_INSERT [TMP_Table] ON
  • 将数据从原始表复制到TMP表
  • SET IDENTITY_INSERT [TMP_Table] OFF
  • DROP原始表格
  • 将TMP_table重命名为原始表名(EXECUTE sp_rename)
  • (backup DB)
  • CREATE TMP_table with columns of original table, but ID column set to identity: false
  • SET IDENTITY_INSERT [TMP_Table] ON
  • copy data from original to TMP table
  • SET IDENTITY_INSERT [TMP_Table] OFF
  • DROP original table
  • Rename TMP_table to original table name (EXECUTE sp_rename)

提示:在SQL Management Studio中更改列并检查发出的脚本(

Tip: change the column in SQL Management Studio and inspect the emitted script (SQL SERVER – Add or Remove Identity Property on Column).

这篇关于您可以使用Entity Framework 6从主键中删除身份吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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