Map string column in Entity Framework to Enum

前端 未结 8 1312
被撕碎了的回忆
被撕碎了的回忆 2021-02-18 13:46

Is there a way to map a string column to an enum in an Entity Model?

I have done this in Hibernate, but can\'t figure it out in EMF.

8条回答
  •  不要未来只要你来
    2021-02-18 14:23

    Acutally I think there is another solution to this.

    What we did in our Project recently was using Extension Methods.

    I wrote two of them, one for the Enum and one for the Entity, but here is the Example:

    namespace Foo.Enums
    {
        [DataContract]
        public enum EAccountStatus
        { 
            [DataMember]
            Online,
            [DataMember]
            Offline,
            [DataMember]
            Pending
        }
    

    ... the enum itself, and now the extension methods containing static class:

        public static class AccountStatusExtensionMethods
        {
    
            /// 
            /// Returns the Type as enumeration for the db entity
            /// 
            /// Entity for which to check the type
            /// enum that represents the type
            public static EAccountStatus GetAccountStatus(this Account entity)
            {
                if (entity.AccountStatus.Equals(EAccountStatus.Offline))
                {
                    return EAccountStatus.Offline;
                }
                else if (entity.AccountStatus.Equals(EAccountStatus.Online))
                {
                    return EAccountStatus.Online;
                }
                else if (entity.AccountStatus.Equals(EAccountStatus.Pending))
                {
                    return EAccountStatus.Pending;
                }
                throw new System.Data.Entity.Validation.DbEntityValidationException(
                    "Unrecognized AccountStatus was set, this is FATAL!");
            }
    

    ... the extension method for the entity type, and a convenience method for shorter typing:

            /// 
            /// Gets the String representation for this enums choosen 
            /// 
            /// Instance of the enum chosen
            /// Name of the chosen enum in String representation
            public static String GetName(this EAccountStatus e)
            {
                return Enum.GetName(typeof(EAccountStatus), e);
            }
        }
    }
    

    ... and finally usage:

    // to set always the same, mappable strings:
    db.AccountSet.Single(m => m.Id == 1).Status = EAccountStatus.Online.GetName();
    
    // to get the enum from the actual Entity you see:
    EAccountStatus actualStatus = db.AccountSet.Single(m => m.Id == 1).GetAccountStatus();
    

    Now, you just need to be "using Foo.Enums;" and you can call the methods on the entity as well as on the enum. And even better, in some sort of wrappers for your entities, you could also do seamless marshalling between defferent types representing the same thing in big projects.

    The only thing worth noting about this is that you sometimes have to execute the extension method before you hand your Linq expression to Linq. The problem here is that Linq can't execute the extension method in its own context...

    Maybe just an alternative, but we've done it like that because it gives you great flexibility on how to get things for entities. You could easily write an extension to receive an Accounts actual Products in ShoppingCart...

    Greetings, Kjellski

提交回复
热议问题