1

How to map enums property in codefirst mapping?

My enum:

public enum GenderType : int { Male = 0, Female = 1 } 

My Model

public string City { get; set; } public string Country { get; set; } public string Occupation { get; set; } public string WebsiteURL { get; set; } public GenderType Gender { get; set; } public int gender { get; set; } public GenderType Gender { 

// NOT WORKING : Can not implicity convert type int .... get { return gender; } set { gender = (int)value; } }

My Configuration

 Property(model => model.Comment) .HasMaxLength(4000) .IsUnicode(); Property(model => model.Culture) .IsOptional() .HasMaxLength(10); ----- HOW TO WRITE GENDER? 

1 Answer 1

1

Enumerations are supported by EF from version 5 (EF Code First 5) going forward so if you are using that it should work automatically, on the other hand if you are using an older version then you can use the solution mentioned Here as a workaround (i used it and it works). The main idea of the solution is that older versions of EF ignore them without any errors so you need to use another property of type int and map that property then the property of the type of enumeration can simply become a wrapper around that property.

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.