实体框架实体中的枚举类型属性?

发布于 2024-09-11 22:29:30 字数 276 浏览 1 评论 0原文

有没有办法可以将生成的实体映射到枚举?

我的意思很简单:

class Person
{
    RelationshipStaus RelationshipStatus { get; set; }
}

enum RelationshipStatus : byte
{
    Single,
    Married,
    Divorced
}

数据库中的属性RelationshipStatus是一个简单的字节,我希望在我的项目中它应该是一个枚举。

Is there a way I can map generated entities to enum?

And what I mean is simple:

class Person
{
    RelationshipStaus RelationshipStatus { get; set; }
}

enum RelationshipStatus : byte
{
    Single,
    Married,
    Divorced
}

The property RelationshipStatus in the DB is a simple byte, I want that in my project it should be an enum.

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

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

发布评论

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

评论(3

看海 2024-09-18 22:29:30

不幸的是,你不能,至少不能直接这样做。为了方便起见,您可以创建一个访问器来将值与枚举类型相互转换:

public int RelationshipStatusInt { get; set; }

public RelationshipStatus RelationshipStatus
{
    get { return (RelationshipStatus)RelationshipStatusInt; }
    set { RelationshipStatusInt = (int)value; }
}

但是,您将无法在 Linq to EF 查询中使用该属性,因为它不会映射到 DB 列(但您可以可以在 Linq to Objects 查询中使用它)。

描述了另一个解决方案 这里,不过感觉有点别扭……

Unfortunately, you can't, at least not directly. For convenience, you can create an accessor that converts the value to and from the enum type:

public int RelationshipStatusInt { get; set; }

public RelationshipStatus RelationshipStatus
{
    get { return (RelationshipStatus)RelationshipStatusInt; }
    set { RelationshipStatusInt = (int)value; }
}

However you won't be able to use that properties in Linq to EF queries, because it won't be mapped to a DB column (but you can use it in Linq to Objects queries).

Another solution is described here, but it feels a bit awkward...

等你爱我 2024-09-18 22:29:30

正如托马斯所说,没有解决办法。

我只是推荐那些希望在 EF 中使用枚举的用户,并投票支持此连接: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions /1015335-枚举支持

As Thomas said, there is no solution.

I would just recommend users who encountered the desire to use enums in EF, and vote for this connection: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1015335-support-for-enums

郁金香雨 2024-09-18 22:29:30

EF ≥ 5 支持枚举。

请参阅

Enums are supported on EF ≥ 5.

See this.

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