如何将自动增量 int 的数据类型更改为 GUID?
我正在尝试更改我定义的 pk 的数据类型,如下所示:
[Id] INT IDENTITY (1, 1) NOT NULL
...给向导。
在我的模型中,我只是更改了数据类型
然后我运行了这两个命令:
add-migration myIntToGuid
update-database
运行 update-database
后,我得到了这个:
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [202101272031026_changeIdToGuid].
Applying explicit migration: 202101272031026_changeIdToGuid.
System.Data.SqlClient.SqlException (0x80131904): Identity column 'Id' must be of data type int, bigint, smallint, tinyint, or decimal or numeric with a scale of 0, unencrypted, and constrained to be nonnullable.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
如何将自动增量 int 的数据类型更改为 GUID?
答案 0 :(得分:0)
如果您像我想的那样使用 SQL Server,则不能将标识列更改为 guid。
标识列必须始终是受支持的类型之一。
<块引用>最常见的方法是通过使用 IDENTITY 列属性或指定唯一标识符 (GUID) 数据类型以及使用 NEWID() 或 NEWSEQUENTIALID() 函数进行默认设置。
或者成为这里的关键词。正如评论中所建议的,请遵循此处的指导:How can I change an int ID column to Guid with EF migration?