实体框架实体中的枚举类型属性?
有没有办法可以将生成的实体映射到枚举?
我的意思很简单:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,你不能,至少不能直接这样做。为了方便起见,您可以创建一个访问器来将值与枚举类型相互转换:
但是,您将无法在 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:
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...
正如托马斯所说,没有解决办法。
我只是推荐那些希望在 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
EF ≥ 5 支持枚举。
请参阅此。
Enums are supported on EF ≥ 5.
See this.