locked
Does EntityFramework4.1 support Enum Type? RRS feed

  • Question

  • When I learn for EntityFramwork4.1 and start with CodeFirst,I wanna to know that does it support Enum Type in POCO,and I also write some code in OnModelCreating method and using FluentAPI such as

     modelBuilder.Entity<Merchant>().Property(m => m.MerchantType)
                                               .HasColumnName("MerchantType")
                                               .HasColumnType("int")
                                               .IsRequired();

    here is my POCO code :

     public class Merchant
        {
            public int ID { get; set; }

            public string Name { get; set; }

            public MerchantType MerchantType { get; set; }
        }

        public enum MerchantType
        {
            Saler=0,Provider=1
        }

    but when Generate my database ,there is no column auto generated in the table. so I wanna to know  does it support Enum Type and how could I generate the right mapping ?

    Tuesday, June 7, 2011 6:57 AM

Answers

  • As i know there is no way to use enums with code first. EF would need to know the underlying datatype like long or int. Its the same pattern as when you work with enumerations in code. For example you ll need to cast to int if you want to operate with a enum value.

    for ef you would mark your merchantType property NOTMAPPED and add wrapperProperties that transform your enum type into a "db valid" type.

     

    you can look here for an example of a wrapper: 

    http://stackoverflow.com/questions/5667473/ef-4-1-code-first-map-enum-wrapper-as-complex-type

     

    greets

    holger

     

    • Marked as answer by Jst.Lee Tuesday, June 7, 2011 8:16 AM
    Tuesday, June 7, 2011 8:09 AM

All replies

  • As i know there is no way to use enums with code first. EF would need to know the underlying datatype like long or int. Its the same pattern as when you work with enumerations in code. For example you ll need to cast to int if you want to operate with a enum value.

    for ef you would mark your merchantType property NOTMAPPED and add wrapperProperties that transform your enum type into a "db valid" type.

     

    you can look here for an example of a wrapper: 

    http://stackoverflow.com/questions/5667473/ef-4-1-code-first-map-enum-wrapper-as-complex-type

     

    greets

    holger

     

    • Marked as answer by Jst.Lee Tuesday, June 7, 2011 8:16 AM
    Tuesday, June 7, 2011 8:09 AM
  • thx a lot! :)
    Tuesday, June 7, 2011 8:16 AM