0

I have a class User as below with an enum field Type. How can I store string value of enum in SQL instead of numeric value?

 public class Student
 {
     public int Id { get; set; }
     public string FullName { get; set; }
     public UserTypeEnum Type { get; set; }
     public List<int> CourseIds { get; set; }
 }

 public enum UserTypeEnum
 {
     Bachelor = 10,
     Master = 11,
     PhD = 12
 }

I have tried to do it using EF Core but it doesn't work.

0

1 Answer 1

0

You can convert your enum to string and retrieve it as enum with code below in your modelbuilder:

 modelBuilder.Entity<Student>().Property(e => e.Type)
            .HasConversion(
                v => v.ToString(),
                v => (UserTypeEnum)Enum.Parse(typeof(UserTypeEnum), v
            ));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.