How can I map an Enum using Entity Framework 4.1 Fluent API?

喜你入骨 提交于 2019-12-13 15:23:45

问题


I've gotten somewhat lost between the sheets of EF... Like the rest of the free world, I really have a need to use Enums in my POCOs. Like many I talk to, I would thoroughly enjoy mapping Enums using code only because, well I just don't like pictures all that much.

My quandary here is that I am getting conflicting information on nearly every article I pull up. It gets even harder sifting out the custom implementations or "extensions" people have published to try and work around the EF shortfall.

The EF June CTP announces support for Enums and Spatial Types but it appears that support only comes when using the designer? And also, is the June CTP part of the EF 4.1 Update 1 or still not RTM?

If there's a way to support Enums using the Fluent API, I would be indebted to anyone who can help me out or steer me in the right direction!

Thanks, Jason


回答1:


Enums are not supported by current EF version. They are supported in June 2011 CTP for both EDMX and code mapping but that CTP doesn't have production version - it is not part of EF 4.1 Update 1 or upcomming EF 4.2. Imho if we will be very lucky it will be part of .NET 4.5.

The easiest way to use enums now is using two properties: mapped int and non mapped enum property converting from mapped int:

public class Test
{
    public int EnumValue { get; set; }
    public EnumType Value 
    {
        get { return (EnumType)EnumValue; }
        set { EnumValue = (int)value; }
    }
}

You cannot use enums in Linq to entities queries when using this approach.




回答2:


Enum is not support in EF at this stage. The latest 4.2 release only has bug fixes. Seem like Enum support will be in EF 5 which will come out with .Net 4.5. Please read http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/67a9247e-eccf-4b14-9da1-db630e408ae8



来源:https://stackoverflow.com/questions/7490530/how-can-i-map-an-enum-using-entity-framework-4-1-fluent-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!