【问题标题】:how to change auto increment int to guid如何将自动增量 int 更改为 guid
【发布时间】:2021-01-27 20:46:57
【问题描述】:

如何将自动增量 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?

【问题讨论】:

  • 为什么不添加详细并共享sql?
  • 另外,底层数据库是什么?
  • 那么这个 202101272031026_changeIdToGuid 脚本是什么样子的?
  • 你看过这个帖子是否有可能的答案stackoverflow.com/questions/36022901/…

标签: c# .net entity-framework entity-framework-6


【解决方案1】:

如果您像我认为的那样使用 SQL Server,则不能将标识列更改为 guid。

标识列必须始终是受支持的类型之一。

https://www.mssqltips.com/sqlservertip/1600/auto-generated-sql-server-keys-with-the-uniqueidentifier-or-identity/

最常见的方法是使用 IDENTITY 列属性或指定唯一标识符 (GUID) 数据类型以及默认使用 NEWID() 或 NEWSEQUENTIALID() 函数。

或者是这里的关键词。正如 cmets 中所建议的,请遵循此处的指导:How can I change an int ID column to Guid with EF migration?

【讨论】:

    猜你喜欢
    • 2014-04-28
    • 2011-07-10
    • 2013-11-07
    • 2013-08-03
    • 2012-09-12
    • 2016-07-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-22
    相关资源
    最近更新 更多