【问题标题】:How to map enum property on codefirst structure?如何在codefirst结构上映射枚举属性?
【发布时间】:2013-07-05 07:30:19
【问题描述】:

如何在codefirst映射中映射枚举属性?

我的枚举:

public enum GenderType : int
{
    Male = 0,
    Female = 1
}

我的模型

public string City { get; set; }
    public string Country { get; set; }
    public string Occupation { get; set; }
    public string WebsiteURL { get; set; }
    public GenderType Gender { get; set; }
    public int gender { get; set; }
    public GenderType Gender
    {

// 不工作:不能隐式转换类型 int .... 获取{返回性别; } 设置{性别=(int)值; } }

我的配置




            Property(model => model.Comment)
                .HasMaxLength(4000)
                .IsUnicode();

            Property(model => model.Culture)
                .IsOptional()
                .HasMaxLength(10);

----- HOW TO WRITE GENDER?

【问题讨论】:

    标签: .net visual-studio-2012 ef-code-first code-first entity-framework-migrations


    【解决方案1】:

    EF 从版本 5(EF Code First 5)开始支持枚举,因此如果您使用它应该自动工作,另一方面,如果您使用的是旧版本,那么您可以使用提到的解决方案 Here 作为一种解决方法(我使用它并且它有效)。该解决方案的主要思想是旧版本的 EF 会忽略它们而不会出现任何错误,因此您需要使用 int 类型的另一个属性并映射该属性,然后枚举类型的属性可以简单地成为该属性的包装器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-05
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多