EnumDataTypeAttribute 应该在使用实体框架的 .NET 4.0 中正常工作吗?
我有一个枚举,我想将其作为某种值保留到底层数据库中,以便我可以来回调用它。
我读过一些文章,建议创建一个枚举包装器,其中定义了静态隐式运算符,并使用 ComplexType 对象映射进行映射,如下面的链接所述。
此解决方案完美运行!感谢 Alex James。
除此之外,我发现了 EnumDataTypeAttribute Class
其目的似乎是通过实体框架处理枚举持久性。我尝试了一下,似乎根本不起作用。这是一个代码示例。
public enum StreetDirection {
East
, None
, North
, NorthEast
, NorthWest
, South
, SouthEast
, SouthWest
, West
}
public enum StreetType {
Avenue
, Boulevard
, Court
, Crescent
, Drive
, Hill
, None
, Road
, Street
}
public class StreetTypeWrapper {
public int Value {
get {
return (int)t;
}
set {
t = (StreetType)value;
}
}
public StreetType EnumValue {
get {
return t;
}
set {
t = value;
}
}
public static implicit operator int(StreetTypeWrapper w) {
return w.Value;
}
public static implicit operator StreetType(StreetTypeWrapper w) {
return w == null ? StreetType.None : w.EnumValue;
}
public static implicit operator StreetTypeWrapper(int i) {
return new StreetTypeWrapper() { Value = i };
}
public static implicit operator StreetTypeWrapper(StreetType t) {
return new StreetTypeWrapper() { EnumValue = t };
}
private StreetType t;
}
public class Street {
[EnumDataType(typeof(StreetDirection))]
public StreetDirection Direction { get; set; }
public string Name { get; set; }
public int StreetId { get; set; }
public StreetTypeWrapper Type { get; set; }
}
public class StreetTypeMapping
: ComplexTypeConfiguration<StreetTypeWrapper> {
public StreetTypeMapping() {
Property(o => o.Value)
.HasColumnName("StreetType");
}
}
现在,如果我相信和/或正确理解 MSDN 关于 EnumDataTypeAttribute 类的内容,则 Direction
属性应该保留到数据库中。嗯,事实并非如此!我找不到原因,除了 EF 不支持枚举持久性。至于 StreetTypeWrapper
及其 StreetTypeMapping
类,它确实可以完美地工作。
是否有任何线索说明为什么 EnumDataType 不能按预期工作?
I have an enumeration which I'd like to persist as a value of some sort into the underlying database so that I can bring it back and forth.
I have read some articles that suggest to create a enumeration wrapper with static implicit operators defined, mapped using a ComplexType object mapping as described in the link below.
This solution works flawlessly! My thanks to Alex James.
Aside, I discovered of the EnumDataTypeAttribute Class
which purpose seems to handle enums persistence through Entity Framework. I tried it and it doesn't seem to work at all. Here's a code sample.
public enum StreetDirection {
East
, None
, North
, NorthEast
, NorthWest
, South
, SouthEast
, SouthWest
, West
}
public enum StreetType {
Avenue
, Boulevard
, Court
, Crescent
, Drive
, Hill
, None
, Road
, Street
}
public class StreetTypeWrapper {
public int Value {
get {
return (int)t;
}
set {
t = (StreetType)value;
}
}
public StreetType EnumValue {
get {
return t;
}
set {
t = value;
}
}
public static implicit operator int(StreetTypeWrapper w) {
return w.Value;
}
public static implicit operator StreetType(StreetTypeWrapper w) {
return w == null ? StreetType.None : w.EnumValue;
}
public static implicit operator StreetTypeWrapper(int i) {
return new StreetTypeWrapper() { Value = i };
}
public static implicit operator StreetTypeWrapper(StreetType t) {
return new StreetTypeWrapper() { EnumValue = t };
}
private StreetType t;
}
public class Street {
[EnumDataType(typeof(StreetDirection))]
public StreetDirection Direction { get; set; }
public string Name { get; set; }
public int StreetId { get; set; }
public StreetTypeWrapper Type { get; set; }
}
public class StreetTypeMapping
: ComplexTypeConfiguration<StreetTypeWrapper> {
public StreetTypeMapping() {
Property(o => o.Value)
.HasColumnName("StreetType");
}
}
Now, if I believe and/or understanding correctly what MSDN says about the EnumDataTypeAttribute
class, the Direction
property should get persisted into the database. Well, it doesn't! I can't find a reason for this, except that EF doesn't support enums persistence. As for the StreetTypeWrapper
and its StreetTypeMapping
class, it does work flawlessly.
Are there any clue why the EnumDataType shouldn't work as expected?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为 .NET 框架的设计缺陷。 .NET 框架包含著名的 System.ComponentModel.DataAnnotations 命名空间,其中定义了多个不同的属性。 .NET 框架的许多不同部分都在使用此命名空间,但它们使用它来实现不同的任务,并且每个此类部分仅使用属性的某些子集。这引起了很多混乱。
EnumDataTypeAttribute
就是这样的例子。此属性仅适用于 ASP.NET 动态数据。它允许您使用此属性标记 int 属性,并且自动生成的 UI 将显示带有枚举值的下拉列表,而不是数字值的文本框。因此存在从 enum 到 int 的映射,但它位于 UI 层而不是模型/持久层。This is because of design flaw in .NET framework. .NET framework contains famous
System.ComponentModel.DataAnnotations
namespace where multiple different attributes are defined. Many different parts of .NET framework are using this namespace but they are using it to achieve different tasks and every such part use only some subset of attributes. This causes a lot of confusion.EnumDataTypeAttribute
is such example. This attribute is only for ASP.NET Dynamic Data. It allows you to mark int property with this attribute and automatically generated UI will show drop down with enum values instead of textbox for numeric values. So there is mapping from enum to int but it is in UI layer not in model / persistence layer.