how to change auto increment int to guid

0

How do I change my data type for my auto-increment int to a GUID?

I'm attempting to change the data type of my pk defined like so:

 [Id]              INT            IDENTITY (1, 1) NOT NULL

...to a guid.

In my model, I've simply changed the data type

enter image description here

I then ran these two commands:

add-migration myIntToGuid
update-database

After running update-database, I got this:

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)

How do I change my data type for my auto-increment int to a GUID?

c#
.net
entity-framework
entity-framework-6
asked on Stack Overflow Jan 27, 2021 by Alex Gordon • edited Feb 1, 2021 by Alex Gordon

1 Answer

0

If you are using SQL Server as I think you do, you can't change the identity column to guid.

The identity column must always be one of the supported types.

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

The most common ways are via the use of the IDENTITY column property or by specifying a uniqueidentifier (GUID) data type along with defaulting with either the NEWID() or NEWSEQUENTIALID() function.

Or being the key word here. As suggested in the comments follow the guidance here: How can I change an int ID column to Guid with EF migration?

answered on Stack Overflow Jan 27, 2021 by Athanasios Kataras • edited Jan 27, 2021 by marc_s

User contributions licensed under CC BY-SA 3.0