无法从PK中删除身份属性 [英] Cant remove identity attribute from PK

查看:78
本文介绍了无法从PK中删除身份属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将其中一个表上的主键的数据类型从int更改为字符串(我不是说我想...)。迁移会产生以下代码:

I need to change the data type of the primary key on one of my tables to string from int (I didn't say I WANT to...). The migration generates this bit of code:

AlterColumn("dbo.Venues", "ID", c => c.String(nullable: false, maxLength: 128));

这会导致SQL Server抱怨数据类型必须是int,bigint等,因为该列是身份列。我添加了以下内容,但它似乎没有效果:

This causes SQL server to complain that the data type must be int, bigint, etc because the column is an identity column. I added the following but it appears to have no effect:

AlterColumn("dbo.Venues", "ID", c => c.Int(identity:false));

问题:如何进行迁移以将数据类型从int更改为string?

Question: How do I get the migration to change the data type from int to string?

以下内容可能与我的问题有关,但不一定:
迁移生成的唯一代码是上面显示的第一个AlterColumn。当以本机生成的方式运行时,迁移导致SQL Server抱怨它无法更改列,因为该列存在依赖性。实际上,唯一的依赖关系是它是PK(没有表将其称为FK)。我将迁移修改为如下所示,现在出现如上所述的数据类型错误。

The following may be related to my question but not necessarily: The only code generated by the migration is the first AlterColumn shown above. When run as natively generated, the migration caused SQL Server to complain that it could not alter the column because there were dependencies on it. In fact the only dependency is that it is a PK (there are no tables that reference it as a FK). I modified the migration to appear as follows and now I am getting the data type error as indicated above.

DropPrimaryKey("dbo.Venues");
AlterColumn("dbo.Venues", "ID", c => c.Int(identity:false));
AlterColumn("dbo.Venues", "ID", c => c.String(nullable: false, maxLength: 128));
AddPrimaryKey("dbo.Venues", "ID");


推荐答案

只能从列中删除身份规范通过重新创建表。查看 ALTER TABLE-ALTER COLUMN 语句:没有语法可以更改(添加或删除)身份规范。

Removing an identity specification from a column can only be done by re-creating the table. Look at the ALTER TABLE - ALTER COLUMN statement: there is no syntax to change (add or remove) an identity specification.

因此,恐怕您必须这样做

So I'm afraid you'll have to do this manually and make a fresh start for subsequent migrations.

BTW Sql Server Management Studio将无法帮助您执行此操作。您可能要使用Toad for Sql Server。删除身份规范后,它将创建一个完整的表重建脚本。

BTW Sql Server Management Studio won't help you doing this. You may want to use Toad for Sql Server. It creates a full table rebuild script when you remove an identity specification.

这篇关于无法从PK中删除身份属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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