实体框架枚举仅作为 POCO
我试图将模型中存在的枚举保存到数据库中,但每次执行实体框架时都会抱怨没有与枚举关联的查找表。我不需要查找表,我只希望枚举存在于代码中并作为整数存储在数据库中。
保存时发生错误 不向外国暴露的实体 他们的关键属性 关系。实体条目 属性将返回 null 因为 单个实体不能被识别为 异常的来源。处理 保存时可以进行例外处理 通过暴露外键更容易 实体类型中的属性。看 有关详细信息,请参阅 InnerException。
“无法将 NULL 值插入 列“类型”,表 '测试.dbo.兴趣';柱子 不允许空值。插入 失败。\r\n该语句已 终止。
Type 的值绝对不为空,EF 只是这样对待它,因为它无法找到我一开始不想要的外键表。
如何让实体框架将枚举视为 int,然后在调用数据库检索模型时转换回来?
I am trying to save an Enum that exists in my model down to the database but everytime I do Entity Framwework complains that there is no look up table associated with the Enum. I do not want a look up table, I just want the enum to exist in code and be stored as integers in the database.
An error occurred while saving
entities that do not expose foreign
key properties for their
relationships. The EntityEntries
property will return null because a
single entity cannot be identified as
the source of the exception. Handling
of exceptions while saving can be made
easier by exposing foreign key
properties in your entity types. See
the InnerException for details."Cannot insert the value NULL into
column 'Type', table
'Test.dbo.Interests'; column
does not allow nulls. INSERT
fails.\r\nThe statement has been
terminated.
The value for Type is definitely not null, EF just treats it that way cause it can't find a Foreign key table that I don't want in the first place.
How do I get entity framework to treat my enum as an int and then convert back out when I call down to the database to retrieve my model?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实体框架根本不支持枚举,因此您无法将枚举属性映射到数据库。您必须使用 int 属性,如果您还想要枚举,则必须定义另一个未映射的属性(如果是自动生成的实体,您必须在分部类中定义它),它将 int 转换为枚举。编辑:
在以后的版本中添加了枚举支持。如果您想映射枚举(整数值,目前不是字符串),您需要使用 .NET 4.5 和 EF5 或 EF6.x 和 .NET 4.x。
Entity framework doesn't support enums at all so you cannot map enum property to the database. You must use int property instead and if you want enum as well you must define another not mapped property (in case of autogenerated entities you must define it in your partial class) which converts int to enum.Edit:
Enum support was added in later version. If you want to map enums (integer values, not strings at the moment) you need to use .NET 4.5 and EF5 or EF6.x and .NET 4.x.
我找到了这种方法并且它有效,它只是不是直接的枚举。您必须创建一个可以隐式来回映射的外观类。它可以工作,但不像我希望的那么顺利。
http:// daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/
在您的 DataContext 中,您需要以下是您的 OnModelCreating 方法。
这告诉实体框架将 int 值映射到数据库中的值。我想如果您想将值作为字符串存储在数据库中,您也可以使用字符串。
I found this way of doing it and it works, it just isn't the Enum directly. You have to create a facade class that can implicitly be mapped back and forth. It works, but not as smooth as I would have liked.
http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/
In your DataContext you need the following in your OnModelCreating method.
This tells Entity Framework to map the int value to the value in the database. I guess you could also use strings if you wanted to store the values as strings in the database.
从 .NET 4.5 附带的实体框架 5 开始,支持枚举作为属性类型。
这适用于代码优先和EF 设计器 场景。
As of Entity Framework 5, which ships with .NET 4.5, enums are supported as types for properties.
This works for both code first and EF designer scenarios.
在我的项目中,我将枚举映射到数据库中的整数列,并限制对 POCO 模型中的属性的访问。就像下面的例子一样。
希望有帮助!
In my projects I am mapping the enum to an integer column in database, and I restrict access to the property in my POCO model. Something like the example below.
Hope it Helps!