0

I am setting up Entity Framework 6 using Code First. Some of my models will be using Enums, and these Enums exist in an external assembly. I know in Model First I was able to specify and external reference to an Enum. Can the same thing be done using Code First?

I've searched the web, but haven't had any luck finding an answer. Any help is appreciated.

2 Answers 2

3

To add to Masoud's answer EF 6 has native support for enums

public Gender Gender {get; set;}

is sufficient.

1

Yes, just reference to your assembly that contains your public enums and use them, for example:

public enum Gender
{
    Male=1,
    Female=2
}

And use it as following:

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Column(Name="Gender")]
    public int InternalGender { get; set; }
    [NotMapped]
    public Gender Gender
    {
       get { return (Gender)this.InternalGender; }
       set { this.InternalGender = (int)value; }
    }
}
1
  • This is exactly how I had it, but was still running into issues. Closed down VS and reopened it, no issues. Go figure :P
    – Will
    Sep 19, 2014 at 13:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.