简体   繁体   English

EF 4.1 CodeFirst和枚举

[英]EF 4.1 CodeFirst and Enumerations

I have a class like that: 我有一个这样的课:

public class Customer
{
  public string Name {get; set; }
  public enumGender Gender {get; set; }
}

public enum enumGender 
{
    Male,
    Female
}

I`m trying to use this as entity and i want to map gender to the database as int, right now gender of course doesn't get mapped at all. 我正在尝试将其用作实体,我想将性别作为整数映射到数据库,现在性别当然根本不会被映射。

any idea how to do that 任何想法如何做到这一点

thanks in advance. 提前致谢。

I was looking for the same thing and came across this link that helped me. 我一直在寻找相同的东西,并且发现了对我有帮助的链接。

http://geekswithblogs.net/JoshReuben/archive/2011/05/18/entity-framework-4.1-code-first--mapping-enums-to-lookup.aspx http://geekswithblogs.net/JoshReuben/archive/2011/05/18/entity-framework-4.1-code-first--mapping-enums-to-lookup.aspx

Hope it helps 希望能帮助到你

this is how i got over it.... 这就是我克服它的方法。

public class Customer
{
  public string Name {get; set; }
        public int gender { get; set; }
        public enumGender Gender
        {
            get { return (CodeFirstEF.Gender) gender; }
            set { gender = (int) value; }
        }
}

public enum enumGender 
{
    Male,
    Female
}

Another option, would be upgrading to the June CTP of Entity Framework, which now supports enums. 另一个选择是升级到现在支持实体枚举的实体框架的六月CTP。 http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=26660 http://www.microsoft.com/download/zh-CN/details.aspx?displaylang=en&id=26660

This has been working wonderfully for me. 这对我来说一直很棒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM