1

如何在 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 .... get { return gender; } 设置 { 性别 = (int) 值;} }

我的配置




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

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

----- HOW TO WRITE GENDER?
4

1 回答 1

1

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

于 2013-07-05T18:15:59.227 回答