映射到 Nhibernate 中的枚举位标志 [英] Mapping to an Enum bit flag in Nhibernate

查看:16
本文介绍了映射到 Nhibernate 中的枚举位标志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用以下枚举标志

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

在 NHibernate 中,我在使用之前已经映射到一个枚举:

In NHibernate I have mapped to a enum before using:

<property type="n.Permssions, n.Permssions"
name="Permssions" column="Permssions"></property>

我的问题是,如何使用 NHibrate 将按位字段映射到标志字段?

My question is, how do I map a bitwise field to a Flag field using NHibnerate?

推荐答案

当我映射一个枚举,并且这个枚举有一个int"类型的支持值时,我只是将我的枚举属性映射到数据库中的一个 int 字段.我没有遇到过这方面的问题.
我也为标志枚举做了这个,这只是没有问题.当您组合某些标志时,NHibernate 会将这些标志的组合"持久化到数据库中的指定列中.
当您检索具有标志枚举"属性的实例时,NHibernate 会将其重组回正确的组合.
例如,如果数据库包含3",则 NHibernate 将使用适当值的组合填充您的属性.
其实都是我让NHibernate自己搞定的:

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" />

其中 OnCallType 列在我的数据库中为 int 类型,而 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.

这篇关于映射到 Nhibernate 中的枚举位标志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆