Mapping to an Enum bit flag in Nhibernate

前端 未结 5 890
北恋
北恋 2020-12-17 14:55

Take the following Enum Flag

[Flags]
enum Permssions
{
   CanComment = 1,
   CanEdit = 2,
   CanDelete = 4,
   CanRemoveUsers = 8,
   All = CanComment | CanE         


        
5条回答
  •  余生分开走
    2020-12-17 15:17

    When I map an enum, and this enum has a backing value of type 'int', I just map my enum property to an int field in the database. I haven't run into problems for this.
    I have done this for flag enums as well, and this just works without problems. When you combine certain flags, NHibernate will persist the 'combination' of those flags into the specified column in the database.
    When you retrieve the instance that has a property of 'flag enums', then NHibernate will reconstitute it back to the correct combination.
    For instance, if the DB contains '3', then NHibernate will populate your property with the combination of the apropriate values.
    In fact, I let NHibernate figure it out all by himself:

    
    

    where the OnCallType column is of type int in my DB, and the OnCallType property is an enumerated type that has the flags attribute.

提交回复
热议问题