'Entity Framework Change Primary Key Type
I'm using EF 6.0 Code First, and I have this entity:
public class TrainingRespect
{
[Key]
public int RespectId { get; set; }
public DateTime? DateWhenRespected { get; set; }
#region
public string UserId { get; set; }
public User User { get; set; }
public Guid TrainingId { get; set; }
public Trening Training { get; set; }
#endregion
}
And I would like to change it's Primary Key (RespectId) from int to GUID / string / long.
What is easiest way of doing this ? Can I just change type and EF migrations will take care of everything or it should be done some other way ?
Solution 1:[1]
Here one solution if you want to changing primary key data type in EF Core:
- Drop the indexes and constraints
- Rename Id to Old Id
- Make new Id of the right datatype
- Run an embedded sql statement to update the foreign keys
- Drop old id
- Create new indexes and constraints
Here is the full article: https://www.codeproject.com/Articles/1193009/Changing-primary-key-data-type-in-EF-Core
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vangi |
