简体   繁体   中英

EF6 Code First - External Enums

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.

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

public Gender Gender {get; set;}

is sufficient.

Yes, just reference to your assembly that contains your public enum s 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; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM