Monday, 1 September 2025

EF Core: Using Enums as Strings

Leave a Comment

Choosing how to store enums in your database is one of the minor choices that can have a big influence when working with Entity Framework Core (EF Core). EF Core saves enums as integers by default. Performance-wise, that's OK, but let's face it, a 2 or 3 in a database field doesn't really tell you anything. What does the number three mean? "Shipped" ? "Canceled" ? To discover out, you would need to look at the code.


However, what if "Shipped" could be stored directly in place of 3? EF Core makes it surprisingly simple to save enums as strings in this situation.

Why Bother Storing Enums as Strings?
It’s Just More Readable

When someone is reviewing the database, whether a support representative, analyst, or even you six months from now, a value like "Delivered" is immediately apparent. You don’t need to guess or cross-reference your codebase.

Easier Debugging and Maintenance

String values make logs, queries, and even reports easier to understand—no need to mentally decode what each number means.

Safer Long-Term
If you ever add or reorder enum members in your code, integer values can suddenly point to the wrong thing. Strings avoid this; the value stays the same regardless of its position in the enum.

How to Do It in EF Core?

Thankfully, EF Core provides a HasConversion() method that makes this process extremely straightforward.

Step-by-Step Example
1. Define an enum

public enum OrderStatus
{
    Pending,
    Processing,
    Shipped,
    Delivered
}

2. Use it in your model

public class Order
{
    public int Id { get; set; }
    public OrderStatus Status { get; set; }
}

3. Add this to your DbContext

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Order>()
        .Property(o => o.Status)
        .HasConversion<string>();
}


That’s it. Now EF Core will store the enum values as strings, such as "Pending" or "Delivered", instead of numbers.

Real-World Example: E-commerce System
Let’s say you’re building an online store. You have an OrderStatus enum.

Pending , Paid , Packed , Shipped , Delivered , Cancelled .

Why does storing as strings help?
Anyone viewing the Orders table can understand the status without needing to dive into the code. Support teams and analysts can filter and search the data more easily. Even if you decide to insert a new status like Delayed , it won't mess up existing data.

When Not to Use Strings?
Let’s be fair — strings aren’t always the best choice.

High-Performance Systems

If you’re building something like a trading platform that stores millions of rows per day, those extra characters add up. Integers are faster to compare and take up less space.

Frequently Changing Enum Names
If you frequently rename your enum values (not just add new ones), string values can break things. The DB still retains the "OldValue" even after you rename it to "NewValue." Currently, there's a mismatch in the code.

Advanced: Custom Value Converters
Need a bit more control? EF Core lets you define custom converters.

var converter = new ValueConverter<OrderStatus, string>(
    v => v.ToString(),
    v => (OrderStatus)Enum.Parse(typeof(OrderStatus), v));

modelBuilder.Entity<Order>()
    .Property(e => e.Status)
    .HasConversion(converter);

This is handy if you want to localize, abbreviate, or format the string differently. 

Entity Framework Core 1.0 Hosting Recommendation

One of the most important things when choosing a good Entity Framework Core 1.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable Entity Framework Core 1.0, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFE.eu, customers can also experience fast Entity Framework Core 1.0 hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFE guarantees 99.9% uptime for Entity Framework Core 1.0. And the engineers do regular maintenance and monitoring works to assure its Entity Framework Core 1.0 hosting are security and always up.

0 comments:

Post a Comment