Provides Entity Framework Core integration for the UUID library, enabling efficient database storage and mapping of UUID values. Features include binary (16 bytes), string (32 characters), and base64 (24 characters) storage formats, global configuration options, and per-property customization through value converters.
$ dotnet add package UUID.Serialization.EntityEntity Framework Core value converters for UUID types. This package provides converters for storing UUID values in different formats:
dotnet add package UUID.Serialization.Entity
Configure all UUID properties in your DbContext to use a specific storage format:
public class MyDbContext : DbContext
{
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
{
// Use binary storage (most efficient, 16 bytes)
configurationBuilder.UseUUIDAsBinary();
// Or use string storage (32 characters)
// configurationBuilder.UseUUIDAsString();
// Or use base64 storage (24 characters)
// configurationBuilder.UseUUIDAsBase64();
}
}
Configure individual properties to use specific storage formats:
public class MyDbContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.Property(e => e.Id)
.HasConversion<UUIDToBytesConverter>();
modelBuilder.Entity<AuditLog>()
.Property(e => e.Id)
.HasConversion<UUIDToStringConverter>();
modelBuilder.Entity<Session>()
.Property(e => e.Id)
.HasConversion<UUIDToBase64Converter>();
}
}
-- For binary storage (most efficient, 16 bytes)
CREATE TABLE Users (
Id INTEGER PRIMARY KEY,
UserId BLOB -- SQLite
-- or: UserId BINARY(16) -- SQL Server
-- or: UserId BYTEA -- PostgreSQL
);
-- For string storage (32 characters)
CREATE TABLE AuditLogs (
Id INTEGER PRIMARY KEY,
UserId CHAR(32) -- Fixed-length string
);
-- For base64 storage (24 characters)
CREATE TABLE Sessions (
Id INTEGER PRIMARY KEY,
UserId VARCHAR(24) -- Base64 encoded
);
Multiple Storage Formats
Flexible Configuration
ConfigureConventionsDatabase Compatibility
Performance Optimized
UseUUIDAsBase64) for a balance between readability and storage size.UseUUIDAsBinary) for the most efficient storage and best performance.UseUUIDAsString) when you need human-readable values or easier debugging.Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the terms of the LICENSE file included in the root directory.