简体   繁体   English

使用具有EF Code First方法的域模型中的枚举值

[英]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. 我在我的MVC应用程序中使用Entity Framework Code First方法,并且我为数据库中的每个表都有一些实体类。 On the other hand, I need to use some lookup values ie 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. 另一方面,我需要使用一些查找值,即性别,状态,我不想创建单独的域模型或表,因此我需要在相关的域模型类或单独的类中定义enum值。 Although there are many samples on the web, I have not found a suitable one for EF and MVC . 虽然网上有很多样本,但我找不到适合EFMVC样本。 Could you please give a sample usage that performs this requirements? 您能否提供满足此要求的示例用法?

Note : I use MVC5 and EF6 . 注:我用MVC5EF6 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): 这是我的实体类,名为Visitor和sample实体,可以在单独的类(.cs文件)或同一类(.cs文件)中定义:

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
    }
}

If you want to use enums with your EF models, you can try following solution: 如果您想在EF模型中使用枚举,可以尝试以下解决方案:

    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. EF将自动转换值,您可以根据需要使用它。

With entity framework 5.0 onwards you can just use your enum : 使用实体框架5.0以后,您可以使用您的枚举:

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 如果您使用EF <5.0,则可以使用映射到byte / int属性的枚举类型属性

public class Visitor
{
    ...

    public byte ReasonOfVisitAsByte { get; set; }

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

    ...
}

PS Regarding your questions : PS关于你的问题:

What will the data type of the enum values 枚举值的数据类型是什么

EF will most likely use int type EF最有可能使用int类型

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. 您不能直接使用字符串,但如果您使用属性并做出一些额外的努力,则可以设置enum直接返回字符串而不是其int值。 You can read more about it here 你可以在这里阅读更多相关信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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