.net 如何在实体框架中使用枚举?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1526339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to work with Enums in Entity Framework?
提问by Rafael Rom?o
What is the best way to work with Enums in Entity Framework?
在实体框架中使用枚举的最佳方式是什么?
Remarks: I'm using EF 3 and Firebird.
备注:我使用的是 EF 3 和 Firebird。
回答by Craig Stuntz
There is a somewhat better way to do it in EF 4. Unfortunately, it won't work in EF 1.
在 EF 4 中有一种更好的方法来做到这一点。不幸的是,它在 EF 1 中不起作用。
Here's another approach.
这是另一种方法。
Update:Realenum support was added in the June 2011 EF CTP.
更新:在2011 年 6 月的 EF CTP 中添加了真正的枚举支持。
回答by Geoff
Update:
Entity Framework now supports Enums nativity.
更新:
实体框架现在支持枚举的诞生。
Original:
This is one of those irritating things about EF. Will not be supporting it yet!
原文:
这是关于 EF 的那些令人恼火的事情之一。暂时不会支持!
Or you can do something like:
或者您可以执行以下操作:
public MyEnum MyEnumProperty
{
get { return (MyEnum) InnerEnumProperty; }
set { InnerEnumProperty = (int) value; }
}
But it makes me feel dirty.
但这让我觉得很脏。
回答by Leniel Maccaferri
This question is a bit old, but let me point you to a more recent material since today we have a newer version of Entity Framework:
这个问题有点老了,但让我向您指出一个更新的材料,因为今天我们有一个更新版本的实体框架:
Video: Entity Framework 5 Enums and Moving Solution from EF 4.3by Julie Lerman
视频:实体框架 5 枚举和来自 EF 4.3的移动解决方案,作者:Julie Lerman
I used this video today to catch up with enumsin Entity Framework. It's a great step by step demonstration. Hope it helps you too.
我今天使用这个视频来赶上enums实体框架。这是一个很好的分步演示。希望对你也有帮助。
There's also this introductory post on Entity Framework Design blog:
Entity Framework Design 博客上还有这篇介绍性文章:
回答by MemeDeveloper
I make heavy use of tables (with default values) in DB of the form
我在表单的 DB 中大量使用表(具有默认值)
CREATE TABLE [dbo].[CommunicationPreferences]
(
[ID] smallint NOT NULL,
[SystemName] nvarchar(50) NOT NULL,
[Description] nvarchar(200) NOT NULL,
)
And I drive my EF4 entities from the DB.
我从数据库驱动我的 EF4 实体。
N.B. I use no views, SPROCS or SQL functions, no complex EF types, just direct table to entity mapping. Then extend my entity partial classes to add additional functionality to keep things DRY.
注意我不使用视图、SPROCS 或 SQL 函数,不使用复杂的 EF 类型,只是直接表到实体映射。然后扩展我的实体部分类以添加其他功能以保持干燥。
For Enums I have a simple T4 template, which I hand a list of tables (of the form above), the .tt file gets fired off whenever I update the EF model from the DB (or if I need to on demand), it grabs the data, and builds Enums e.g.
对于 Enums,我有一个简单的 T4 模板,我将表格列表(上面的形式)交给它,每当我从数据库更新 EF 模型时(或者如果我需要),.tt 文件就会被触发,它获取数据,并构建枚举,例如
/// <summary>
/// Enums For The dbo Schema
/// </summary>
public enum CommunicationPreferencesList : short
{
/// <summary>
/// HTML Emails
/// </summary>
[EnumTextValue(@"HTML Emails")]
HTMLEmail = 1,
/// <summary>
/// Plain Text Emails
/// </summary>
[EnumTextValue(@"Plain Text Emails")]
PlainEmail = 2,
/// <summary>
/// Mobile Telephone
/// </summary>
[EnumTextValue(@"Mobile Telephone")]
Mobile = 3,
/// <summary>
/// Landline Telephone
/// </summary>
[EnumTextValue(@"Landline Telephone")]
Landline = 4,
/// <summary>
/// SMS
/// </summary>
[EnumTextValue(@"SMS")]
SMS = 5,
}
Then when I am dealing with an FK ID column / Property on some entity e.g.
然后当我处理某个实体的 FK ID 列/属性时,例如
Users.CommunicationPreferenceID
I simply cast either the ID or the enum for the comparison. e.g.
我只是简单地将 ID 或枚举转换为比较。例如
CommunicationPreferencesList usersPreference = (CommunicationPreferencesList)currentUser.CommunicationPreferenceID;
if(usersPreference == CommunicationPreferencesList.SMS)
{
//send SMS
}
else if(usersPreference == CommunicationPreferencesList.Mobile)
{
//ring the phone
}
I then have some simple helpers to e.g. give the EnumTextValue from an enum instance e.g.
然后我有一些简单的助手,例如从枚举实例中给出 EnumTextValue,例如
string chosenMethod = EntityHelper.GetEnumTextValue(CommunicationPreferencesList.Mobile);
=> "Mobile Telephone"
I find this simple, hassle free, transparent, easy to use, and for my purposes it works a treat and I am happy. I don't see the need to totally mask the numeric value in the DB / entity from the consuming code, I am quite happy to cast one or other of the values, and end up with pretty clean readable code, plus the nice little extra of the EnumTextValue which is generated from the [Description] field in the DB.
我觉得这很简单,没有麻烦,透明,易于使用,对我来说,它是一种享受,我很高兴。我不认为需要从消费代码中完全屏蔽 DB/实体中的数值,我很高兴投射一个或其他值,并最终得到非常干净可读的代码,加上漂亮的小额外从数据库中的 [Description] 字段生成的 EnumTextValue。
回答by Christophe Lambrechts
I had a similar problem and solved it by writing an extension on the entity through the partial class mechanism. I just added a property that does the casting of DB field already in the entity, in our case just an integer.
我有一个类似的问题,并通过部分类机制在实体上编写扩展来解决它。我刚刚添加了一个属性来转换实体中已有的 DB 字段,在我们的例子中只是一个整数。
Only pitfall is to add an ignore serialization attribute, for example when using in combination with WCF.
唯一的缺陷是添加了忽略序列化属性,例如与 WCF 结合使用时。

