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
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?
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.
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?
User contributions licensed under CC BY-SA 3.0