简体   繁体   English

如何使用EF将int映射到枚举

[英]How to map int into enum with EF

Is there anyway to map int field into enum in EFv1? 无论如何,将int字段映射到EFv1中的枚举? Thanks! 谢谢! I want my entity to have enum field rather than int field. 我希望我的实体具有枚举字段而不是int字段。

Create two properties. 创建两个属性。 One mapped to EF, one as a wrapper 一个映射到EF,一个作为包装器

[EdmScalarProperty]
public int EnumPropInteger {get;set}
public MyEnum EnumProp
{
    get { return (MyEnum) EnumPropInteger; }
    set { EnumPropInteger = (int)value; }
}

Not a nice way because you have two public properties but a way. 这不是一个好方法,因为您有两个公共属性,但是有一个方法。

You can simply cast the int to the Enum like this: 您可以像这样简单地将int强制转换为Enum:

public enum TestEnum
{
Zero = 0,
One,
Two
}

TestEnum target = (TestEnum)1;

Target should then contain TestEnum.One; 然后目标应包含TestEnum.One;

Edit: My bad, did not interpret properly at first. 编辑:我不好,起初没有正确解释。 You want the map to handle the cast for you, right? 您希望地图为您处理演员,对吗? Don't know that right now, would have to experiment a bit. 现在不知道该做些什么。

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

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