Posts

Showing posts from April, 2025

Using GUIDs with EF Core

Introduction GUIDs , also known as UUIDs , are a very useful data type that is present in all major databases. It allows having values that are magically generated and never repeat, making them ideal for usage across data sources, where a single source of data cannot be reused. in .NET, they are represented by the Guid type; in SQL Server, it is UNIQUEIDENTIFIER , in PostgreSQL it is UUID , in Oracle it is RAW(16) , and in MySQL it is BINARY(16) . EF Core supports all of them. There are pros and cons regarding using GUIDs as database primary keys: They are obviously great for uniqueness They can be generated on the client But, on the other side: They take a lot of space (16 bytes), compared to 4 bytes for integers or 8 for longs They can lead to very fragmented indexes Let's see what we can do about that. Primary Key Generation in EF Core There are 3 ways to have EF Core generate GUIDs for the primary keys: Have the primary key property marked as database generated of type identit...