如何在实体框架中使用枚举?

发布于 2024-08-07 18:36:01 字数 60 浏览 3 评论 0原文

在实体框架中使用枚举的最佳方法是什么?

备注:我使用的是 EF 3 和 Firebird。

What is the best way to work with Enums in Entity Framework?

Remarks: I'm using EF 3 and Firebird.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下客 2024-08-14 18:36:01

有一个在 EF 4 中实现此操作的更好方法。不幸的是,它在 EF 1 中不起作用。

这里是 另一种方法

更新:Real枚举支持已在2011 年 6 月 EF CTP

There is a somewhat better way to do it in EF 4. Unfortunately, it won't work in EF 1.

Here's another approach.

Update: Real enum support was added in the June 2011 EF CTP.

牵你手 2024-08-14 18:36:01

更新:
实体框架现在支持枚举原生。

原文:
这是 EF 令人恼火的事情之一。暂时还不会支持!

或者你可以这样做:

public MyEnum MyEnumProperty  
{  
  get { return (MyEnum) InnerEnumProperty; }  
  set { InnerEnumProperty = (int) value; }  
}

但这让我感觉很肮脏。

Update:
Entity Framework now supports Enums nativity.

Original:
This is one of those irritating things about EF. Will not be supporting it yet!

Or you can do something like:

public MyEnum MyEnumProperty  
{  
  get { return (MyEnum) InnerEnumProperty; }  
  set { InnerEnumProperty = (int) value; }  
}

But it makes me feel dirty.

赠我空喜 2024-08-14 18:36:01

这个问题有点老了,但让我向您指出更新的材料,因为今天我们有了更新版本的实体框架:

视频:来自 EF 4.3 的实体框架 5 枚举和移动解决方案 作者:Julie Lerman

我使用了这个今天观看视频来了解实体框架中的枚举。这是一个很棒的逐步演示。希望它也能帮助你。

实体框架设计博客上还有这篇介绍性文章:

实体框架中的枚举支持

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.3 by Julie Lerman

I used this video today to catch up with enums in Entity Framework. It's a great step by step demonstration. Hope it helps you too.

There's also this introductory post on Entity Framework Design blog:

Enumeration Support in Entity Framework

如日中天 2024-08-14 18:36:01

我在 DB 中大量使用表格(带有默认值)

CREATE TABLE [dbo].[CommunicationPreferences]
(
    [ID] smallint NOT NULL,
    [SystemName] nvarchar(50) NOT NULL,
    [Description] nvarchar(200) NOT NULL,
)

并且我从 DB 驱动我的 EF4 实体。

注意:我没有使用视图、SPROCS 或 SQL 函数,没有复杂的 EF 类型,只是直接表到实体映射。然后扩展我的实体部分类以添加附加功能以保持干燥。

对于枚举,我有一个简单的 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,

}

然后,当我处理某个实体上的 FK ID 列/属性时,例如,

Users.CommunicationPreferenceID

我只需转换 ID 或枚举以进行比较。例如,

CommunicationPreferencesList usersPreference = (CommunicationPreferencesList)currentUser.CommunicationPreferenceID;

if(usersPreference == CommunicationPreferencesList.SMS)
{
//send SMS
}
else if(usersPreference == CommunicationPreferencesList.Mobile)
{
//ring the phone
}

然后我有一些简单的帮助程序,例如从枚举实例中给出 EnumTextValue 例如,

string chosenMethod = EntityHelper.GetEnumTextValue(CommunicationPreferencesList.Mobile);

 => "Mobile Telephone"

我发现这个简单、无麻烦、透明、易于使用,对于我的目的来说,它是一种享受,我很高兴。我不认为有必要从消费代码中完全屏蔽数据库/实体中的数值,我很高兴转换一个或其他值,并最终得到非常干净的可读代码,再加上漂亮的小额外内容从数据库中的 [Description] 字段生成的 EnumTextValue 的值。

I make heavy use of tables (with default values) in DB of the form

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.

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.

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.

/// <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.

Users.CommunicationPreferenceID

I simply cast either the ID or the enum for the comparison. e.g.

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.

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.

过气美图社 2024-08-14 18:36:01

我遇到了类似的问题,并通过通过部分类机制在实体上编写扩展来解决它。我刚刚添加了一个属性,用于对实体中已有的数据库字段进行转换,在我们的例子中只是一个整数。

唯一的陷阱是添加忽略序列化属性,例如与 WCF 结合使用时。

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.

Only pitfall is to add an ignore serialization attribute, for example when using in combination with WCF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文