EF 上的枚举属性映射到 varchar 列 2011 年 6 月 CTP

发布于 2024-12-07 05:02:11 字数 164 浏览 4 评论 0原文

我正在尝试使用“代码优先”方法创建一个模型。 我想知道是否可以将 Enum 属性映射到数据库中的 varchar 列。 我知道你可以用 byte、small、int、long 来做到这一点。

这是一个旧数据库的项目,它有许多带有约束的列。如果将此结构映射到枚举值,那就太好了。

谢谢

I'm trying to create a model with Code First approach.
I wanted to know if it's possible to map an Enum property to a varchar column in the database.
I know you can do it with byte, small, int, long.

This is a project for an old database and it has many columns with constraints. It would be nice to have this structure mapped to enum values.

Thanks

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

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

发布评论

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

评论(1

山有枢 2024-12-14 05:02:11

不,这是不可能的。 EF 2011 年 6 月 CTP 中的枚举只能与 .NET 平台本身支持的枚举一起使用,并且 .NET 仅支持除 char 之外的整型类型的枚举。这意味着仅支持 byte、sbyte、short、ushort、int、uint、long 或 ulong。 EF 中的映射也是如此。 EF 只能将枚举映射到整数类型的列。

您可以在此处查看更多信息。它还包含 EF 支持的枚举内部类型的描述:

与 CLR 枚举类似,EF 枚举具有基础类型,即
Edm.SByte、Edm.Byte、Edm.Int16、Edm.Int32 或 Edm.Int64 的
如果没有,Edm.Int32 是默认的基础类型
指定。

您也可以查看我在那篇文章下的评论。

No it is not possible. Enum in EF June 2011 CTP can work only with enums supported by .NET platform itself and .NET supports enum only on integral types except char. That means only byte, sbyte, short, ushort, int, uint, long, or ulong are supported. The same is true about mapping in EF. EF can map enum only to column with integer type.

You can check more about that here. It also contains description of internal types supported by EF for enum:

Similarly to CLR enums the EF enums have underlying type which is one
of Edm.SByte, Edm.Byte, Edm.Int16, Edm.Int32 or Edm.Int64 with
Edm.Int32 being the default underlying type if none has been
specified.

You can also check my comment under that article.

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