【问题标题】:Entity Framework Enums as POCO only实体框架枚举仅作为 POCO
【发布时间】:2011-05-20 14:46:30
【问题描述】:

我正在尝试将模型中存在的 Enum 保存到数据库中,但每次执行 Entity Framwework 时都会抱怨没有与 Enum 关联的查找表。我不想要一个查找表,我只希望枚举存在于代码中并作为整数存储在数据库中。

保存时出错 不暴露外国的实体 他们的关键属性 关系。实体条目 属性将返回 null,因为 单个实体不能被识别为 异常的来源。处理 可以进行保存时的例外情况 通过暴露外键更容易 实体类型中的属性。看 InnerException 了解详情。

"无法将值 NULL 插入 “类型”列,表格 'Test.dbo.Interests';柱子 不允许空值。插入 失败。\r\n语句已被 终止。

Type 的值绝对不是 null,EF 只是这样对待它,因为它找不到我一开始不想要的外键表。

如何让实体框架将我的枚举视为 int,然后在我调用数据库检索我的模型时转换回来?

【问题讨论】:

标签: database entity-framework c#-4.0 poco


【解决方案1】:

实体框架根本不支持枚举,因此您无法将枚举属性映射到数据库。您必须改用 int 属性,如果您还想要 enum,则必须定义另一个未映射的属性(如果是自动生成的实体,您必须在部分类中定义它)将 int 转换为 enum。

编辑: 在更高版本中添加了枚举支持。如果您想映射枚举(整数值,目前不是字符串),您需要使用 .NET 4.5 和 EF5 或 EF6.x 和 .NET 4.x。

【讨论】:

  • 枚举已被讨论为未来版本:blogs.msdn.com/b/adonet/archive/2011/05/18/…
  • 查看最新版本的实体框架中的枚举支持。
  • @juFo:当然,但这是 2011 年不支持枚举的答案。线程中有另一个答案提到枚举支持是在 .NET 4.5 + EF5 中添加的。
  • @LadislavMrnka 这篇文章是我搜索结果中的建议答案,但它不再相关。您可以将答案调整为“EF 在旧版本中根本不支持枚举”或类似内容。
【解决方案2】:

我发现了这种方法并且它有效,它不是直接的枚举。您必须创建一个可以隐式来回映射的外观类。它可以工作,但没有我想要的那么顺利。

http://daniel.wertheim.se/2010/06/09/dealing-with-enumerations-and-entity-framework-4-code-first/

public enum InterestTypes
{
    [Description("Attraction - for sightseers and adventurers.")] Attraction = 1,
    [Description("Event - fun for everyone (limited time).")] Event = 2,
    [Description("Recreation - parks, hiking, movies...")] Recreation = 3,
    [Description("Restaurant - good eats.")] Restaurant = 4
}

public class InterestType
{
    private InterestType()
        : this(default(InterestTypes))
    {
    }

    public InterestType(int value)
    {
        Value = value;
    }

    public InterestType(InterestTypes type)
    {
        Value = (int) type;
    }

    public int Value { get; private set; }

    public static implicit operator int(InterestType type)
    {
        return type.Value;
    }

    public static implicit operator InterestType(int value)
    {
        return new InterestType(value);
    }

    public static implicit operator InterestTypes(InterestType type)
    {
        return (InterestTypes) type.Value;
    }

    public static implicit operator InterestType(InterestTypes type)
    {
        return new InterestType(type);
    }
}

在您的 DataContext 中,您的 OnModelCreating 方法中需要以下内容。

   protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.ComplexType<InterestType>()
            .Property(o => o.Value)
            .HasColumnName("Type");
    }

这告诉 Entity Framework 将 int 值映射到数据库中的值。如果您想将值作为字符串存储在数据库中,我想您也可以使用字符串。

【讨论】:

    【解决方案3】:

    从 .NET 4.5 附带的 Entity Framework 5 开始,enums are supported as types for properties

    这适用于code firstEF designer 场景。

    【讨论】:

      【解决方案4】:

      在我的项目中,我将枚举映射到数据库中的整数列,并限制对我的 POCO 模型中的属性的访问。类似于下面的示例。

      public enum MyEnum
      {
          EnumProp1,
          EnumProp2
      }
      
      public class MyEntity
      {
          public long Id{get;set;}
      
          [Column("MyEnum")]
          public int IdMyEnum{ get;protected set;}
      
          [NotMapped]
          public MyEnum
          {
               get{ return (MyEnum)this.IdMyEnum; }
               set{ this.IdMyEnum = (int)value; }
          }
       }
      

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-08
        • 1970-01-01
        • 2012-03-22
        相关资源
        最近更新 更多