Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using enum values in Domain Model with EF Code First approach

I use Entity Framework Code First approach in my MVC application and I have some entity classes for every table in the database. On the other hand, I need to use some lookup values i.e. gender, status for which I do not want to create a separate domain model or table and for this reason I need to define enum values in a related domain model class or a separate class. Although there are many samples on the web, I have not found a suitable one for EF and MVC. Could you please give a sample usage that performs this requirements?

Note : I use MVC5 and EF6. Here is my entity class called Visitor and sample entity that can be defined in a separate class (.cs file) or in the same class (.cs file):

namespace MyProject.Entities
{
    public class Visitor
    {
        [Key]
        public int VisitorID { get; set; }

        public string VisitorName { get; set; }

        //[ForeignKey("ReasonOfVisit")]
        public byte ReasonOfVisit { get; set; }

        //code omitted for brevity
    }


    public enum ReasonOfVisit
    {
        NotSet = 0,
        Business meeting = 1,
        Periodic visit = 2,
        Getting information = 3,
        Leaving package = 4
    }
}
like image 849
Jack Avatar asked Dec 15 '15 08:12

Jack


2 Answers

If you want to use enums with your EF models, you can try following solution:

    public class User
    {
        public string Id { get; set; }

        public RegistrationStatus RegistrationStatus { get; set; }
    }

    public class UserConfiguration : EntityTypeConfiguration<User>
    {
        public UserConfiguration()
        {
                this.Property(x => x.RegistrationStatus)
                .HasColumnType("int")
                .IsRequired();
        }
    }

    public enum RegistrationStatus
    {
        Pending = 1,
        Active = 2,
        Blocked = 3
    }

Of course it is simplified as much as I can. What you basically do is to map your enum to some primitive type. EF will convert values automatically and you can use it as you would like to.

like image 182
kamil-mrzyglod Avatar answered Nov 11 '22 01:11

kamil-mrzyglod


With entity framework 5.0 onwards you can just use your enum :

namespace MyProject.Entities
{
    public enum ReasonOfVisit
    {
        NotSet = 0,
        For business reason = 1,
        For control = 2,
        For lefting package = 3,
        For leisure = 4
    }

    public class Visitor
    {
        ...

        public ReasonOfVisit ReasonOfVisit { get; set; }

        ...
    }
}

If you're using EF < 5.0 you can use enum type property mapped to byte/int property

public class Visitor
{
    ...

    public byte ReasonOfVisitAsByte { get; set; }

    public ReasonOfVisit ReasonOfVisit { get { return (ReasonOfVisit)ReasonOfVisitAsByte; }  
                                         set { ReasonOfVisitAsByte = (byte)value; } } 

    ...
}

P.S. Regarding your questions :

What will the data type of the enum values

EF will most likely use int type

How can I define string values in this enum

You cannot use strings directly but if you use attributes and make some additional efforts it is possible to set that enum returns string directly and not its int value. You can read more about it here

like image 44
Fabjan Avatar answered Nov 11 '22 03:11

Fabjan