映射在NHibernate的一个枚举位标志 [英] Mapping to an Enum bit flag in Nhibernate

查看:122
本文介绍了映射在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>



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

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

推荐答案

当我映射一个枚举,这种枚举具有类型'诠释'的后盾价值,我只是地图我的枚举属性数据库中的一个int领域。我还没有碰到这个问题。


我已经这样做了标志枚举为好,这只是工作没有任何问题。
当你把某些标志,NHibernate的会持续对这些标志的组合到数据库指定列。


当您检索具有'标志枚举属性的实例,然后将NHibernate的重组它回到正确组合。

例如,如果数据库包含'3',然后NHibernate的将填充你的属性与apropriate值的组合。


事实上,我让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类型在我的DB和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天全站免登陆