Entity Framework Core does not natively support C# union types, but developers can employ workarounds to map them effectively. In this guide, Zoran Horvat demonstrates practical techniques for persisting discriminated unions using EF Core's flexible configuration options.
Key strategies include using owned types for simple unions, TPH (Table per Hierarchy) inheritance for more complex scenarios, and custom value converters for lightweight approaches. Each method balances readability, performance, and database schema impact.
For example, a Result<T> union type representing success or failure can be mapped using a discriminator column with TPH, while a simple Option<T> can leverage owned types to avoid extra tables. Horvat also discusses pitfalls like query limitations and provides patterns to keep code clean.
This practical advice helps .NET developers leverage union types without sacrificing ORM efficiency, making domain models more expressive while maintaining data integrity.