简体   繁体   English

实体框架枚举和字符串关联

[英]Entity Framework Enum and string association

I want to use the method in this article to implement friendlier ToString() outputs for my enum types. 我想使用本文中的方法为我的枚举类型实现更友好的ToString()输出。 I would like to know how this can be done in Entity Framework's auto generated Enum codes? 我想知道在Entity Framework的自动生成的Enum代码中如何做到这一点? Would I have to modify the code generation template (if so, can someone kindly give me some guidance since the template is rather complicated), or can someone suggest an alternative method? 我是否必须修改代码生成模板(如果是这样,有人可以给我一些指导,因为模板相当复杂),或者有人可以提出替代方法吗?

Thanks! 谢谢!

You can use your own enum type in your EF model, instead of creating a new enum in the model designer. 您可以在EF模型中使用自己的枚举类型,而不是在模型设计器中创建新的枚举。 Here's how: 这是如何做:

In the model designer, rght click on surface and select: 在模型设计师中,点击表面并选择:

Add New -> Enum Type... 添加新 - >枚举类型...

In the dialog, just set checkbox: 在对话框中,只需设置复选框:

Reference external type 参考外部类型

and enter your type: namespace.MyEnum 并输入您的类型: namespace.MyEnum

Then create columns in your tables to use this type. 然后在表中创建列以使用此类型。

Since you're likely going to modify the existing model, make sure there is no confusion between enum type from the model and (external) enum type from your code. 由于您可能要修改现有模型,因此请确保模型中的枚举类型与代码中的(外部)枚举类型之间没有混淆。 Best approach would be to remove the enum type you previously had created in the model and adjust the columns to use the associated enum type from your code. 最好的方法是删除先前在模型中创建的枚举类型,并调整列以使用代码中的关联枚举类型。

Now, you can declare your enum type with description attributes as you planned. 现在,您可以按计划声明具有描述属性的枚举类型。

You don't need to make workarounds for enums. 您无需为枚举制作变通方法。 They're supported in the latest Entity Framework. 它们在最新的实体框架中得到支持。

To make your enums friendly to your website you can use attributes. 为了使您的网站对您的网站友好,您可以使用属性。 Here is sample attribute: 这是示例属性:

public class EnumDescription : Attribute
{
    public string Text { get; private set; }

    public EnumDescription(string text)
    {
        this.Text = text;
    }
}

Mark your enums with attribute: 使用属性标记您的枚举:

public enum DaylightSavingTime
{
    [EnumDescription("Detect automatically")]
    Auto = 0,
    [EnumDescription("DST always on")]
    AlwaysOn = 1,
    [EnumDescription("DST always off")]
    AlwaysOff = 2
}

Add extensions to enable ToDescription() method: 添加扩展以启用ToDescription()方法:

public static class EnumExtensions
{
    public static string ToDescription(this Enum enumeration)
    {
        Type type = enumeration.GetType();
        MemberInfo[] memInfo = type.GetMember(enumeration.ToString());

        if (null != memInfo && memInfo.Length > 0)
        {
            object[] attrs = memInfo[0].GetCustomAttributes(typeof(EnumDescription), false);
            if (null != attrs && attrs.Length > 0)
                return ((EnumDescription)attrs[0]).Text;
        }

        return enumeration.ToString();
    }
}

Usage: 用法:

var blabla = DaylightSavingTime.Auto;
Console.WriteLine(blabla.ToDescription());

Output: 输出:

Detect automatically 自动检测

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

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