是否可以在EF数据库中使用Enum的字符串值? [英] Is it possible to use the string value of an Enum in an EF database?

查看:92
本文介绍了是否可以在EF数据库中使用Enum的字符串值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中有可爱的描述性枚举,但是当持久化时,使用它们的记录会以其整数值保留。我更喜欢字符串值。

I have lovely descriptive enums in my code, but when persisted, records using those are persisted with their integer values. I would prefer the string value.

推荐答案

似乎没有直接因为枚举只能映射到Byte,Int16,Int32,Int64或SByte,但你可以读到这个: https://entityframework.codeplex.com/discussions/451445 [ ^ ]
It seems not directly since enums can only be mapped to Byte, Int16, Int32, Int64 or SByte, but you can read this : https://entityframework.codeplex.com/discussions/451445[^]


您可以通过多种方式进行操作。请参阅我的文章:

​​枚举类型做不列举!解决.NET和语言限制 [ ^ ],

人类可读的枚举元数据 [ ^ ]。



欢迎您提出后续问题。



祝你好运。

-SA
You can, in a number of ways. Please see my articles:
Enumeration Types do not Enumerate! Working around .NET and Language Limitations[^],
Human-readable Enumeration Meta-data[^].

Your follow-up questions will be welcome.

Good luck.
—SA


我现在正在这样做。我担心,将值限制为enun名称会更加困难。我必须进行POCO方验证。
I am doing that now. My worry there is it's harder to constrain the values to the enun names. I have to do POCO side validation.


这篇关于是否可以在EF数据库中使用Enum的字符串值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆