Created
July 16, 2024 21:46
-
-
Save Muhammad-1990/8fe0754579e7a9b31e06fb234f05b084 to your computer and use it in GitHub Desktop.
Using EF Core to convert Enums to string.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public enum CardStatus : int | |
| { | |
| Active = 1, | |
| Expired = 2, | |
| PendingVerification = 3 | |
| } | |
| public record CardEntity(CardStatus Status); | |
| public class CardConfiguration : IEntityTypeConfiguration<Card> | |
| { | |
| public void Configure(EntityTypeBuilder<Card> builder) => | |
| builder.Property(x => x.Status).HasConversion<string>(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
EF Core contains many pre-defined conversions that avoid the need to write conversion functions manually. This is perfect for converting an Enum because it will automatically convert it to string when the provider type is configured as string using the generic type of HasConversion.