Enum - saving to property / field

Dean Everhart 1,541 Reputation points
2023-03-13T16:55:35.7666667+00:00

Premise:
An enum in a model does not create a property / field to hold the enum value.

Question:
How do you have this enum value saves into a property / field?

What does declaring an enum value accomplish if it is not creating a corresponding property / field that saves the value?

        public enum Option : byte
        { Option1, Option2, Both};
Developer technologies ASP.NET ASP.NET Core
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2023-03-13T17:46:03.74+00:00

    An enum is a named constant. Enums are used like any constant.

    OptionTest option = new OptionTest();
    option.Value = Option.Option1;
    
    public enum Option : byte
    { 
        Option1, 
        Option2,
        Both 
    };
    
    public class OptionTest
    {
        public Option Value { get; set; }
    }
    
    

    Your expectation is not clear but if you are trying to figure out how to enums with Entity Framework then see if the following helps.

    Value Conversions

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-03-13T18:21:04.4666667+00:00

    enum are value types, based on a numeric with a defined set of values. once you define a enum type, you just use the type name. they can be cast as their base type (byte in your case) for your example Option:

    public class MyClass
    {
         private Option _optionPrivate = Option.Option1;
         public Option OptionField  = Option.Option1;
         public Option OptionProperty {get; set;} = Option.Option1
    }
    

    these all define a byte enum typed as Option

    0 comments No comments

  2. Anonymous
    2023-03-14T06:35:37.59+00:00

    Hi @Dean Everhart

    By default, for the Enum property, EF core will create the column with int type:

    For example:

    Using the following class, after generating the table via EF core migration, the customer table will generate a MembershipType column with int type. When insert new customer, the MembershipType column value is: 0, 1, 2 ...

        public class Customer
        {
            [Key]
            public int Id { get; set; }
    
            public string FirstName { get; set; }
    
            public string LastName { get; set; }
    
            public MembershipType MembershipType { get; set; }
        }
        public enum MembershipType
        {
            Free,
            Premium,
            SuperVIP
        }
    

    The result:

    User's image

    If change the enum as below:

        public enum MembershipType:byte
        {
            Free,
            Premium,
            SuperVIP
        }
    

    After migration, the MembershipType column is tinyint type: When insert new customer, the MembershipType column value is: 0, 1, 2...

    User's image

    If you want to save the enum value as string format. You can add the value conversion in the OnModelCreating method, like this:

            public DbSet<Customer> Customers { get; set; } 
            protected override void OnModelCreating(ModelBuilder modelBuilder)
            {
                base.OnModelCreating(modelBuilder);
                modelBuilder.Entity<Customer>()
                .Property(u => u.MembershipType)
                .HasConversion<string>()
                .HasMaxLength(50);
            }
    

    The result as below: and when add customer, the MembershipType column value is: Free, Premium and SuperVIP.

    User's image

    Besides, you can also refer this screenshot:

    image3


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.