如何用EF Code First解释一个枚举类型 [英] How is interpreted an enum type with EF Code First

查看:165
本文介绍了如何用EF Code First解释一个枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个模型:

Public class Person
{
    [Key]
    Public int PersonId { get; set: }
    Public int Age { get; set; }
    Public ColorEnum FavoriteColor { get; set; }
}

Public Enum ColorEnum
{
    Red = 1,
    Green = 2,
    Blue = 3
}

实体框架代码首先可以使用Person模型生成相应的表吗? ColorEnum类型如何?

Is it possible for Entity Framework Code First to use the Person model to generate the corresponding table? What about the ColorEnum type?

感谢

推荐答案

EF 4.3不支持枚举。但是已经宣布 枚举支持是与EF 5 一起出现的,这与.NET 4.5一起出现。要处理使用Code-First的枚举,您将执行以下操作:

EF 4.3 doesn't support Enums. But is has been announced that Enum support is coming with EF 5, which is due out alongside .NET 4.5. To handle enums with Code-First currently you'll do something like the following:

Public class Person
{
    [Key]
    Public int PersonId { get; set: }
    Public int Age { get; set; }

    public int FavoriteColorValue{ get; set;}
    [NotMapped]
    Public ColorEnum FavoriteColor 
    { 
        get{ return (ColorEnum)FavoriteColorValue; } 
        set{ FavoriteColorValue = (int)value; } 
    }
}

Public Enum ColorEnum
{
    Red = 1,
    Green = 2,
    Blue = 3
}

这篇关于如何用EF Code First解释一个枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆