Skip to content

Instantly share code, notes, and snippets.

@Muhammad-1990
Created July 16, 2024 21:46
Show Gist options
  • Select an option

  • Save Muhammad-1990/8fe0754579e7a9b31e06fb234f05b084 to your computer and use it in GitHub Desktop.

Select an option

Save Muhammad-1990/8fe0754579e7a9b31e06fb234f05b084 to your computer and use it in GitHub Desktop.
Using EF Core to convert Enums to string.
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>();
}
@Muhammad-1990

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment