Take the following Enum Flag
[Flags]
enum Permssions
{
CanComment = 1,
CanEdit = 2,
CanDelete = 4,
CanRemoveUsers = 8,
All = CanComment | CanEdit | CanDelete | CanRemoveUsers
}
In NHibernate I have mapped to a enum before using:
<property type="n.Permssions, n.Permssions"
name="Permssions" column="Permssions"></property>
My question is, how do I map a bitwise field to a Flag field using NHibnerate?
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:
<property name="OnCallType" column="OnCallType" />
where the OnCallType column is of type int in my DB, and the OnCallType property is an enumerated type that has the flags attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With