如何使用NHibernate持久化枚举 [英] How to persist an enum using NHibernate

查看:197
本文介绍了如何使用NHibernate持久化枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用NHibernate将枚举持久化到数据库?这是一个表的枚举中的每个值的代码和名称。



我想保留枚举没有一个实体,但仍然有一个外键(枚举的int表示)从所有其他引用实体到枚举的表。

解决方案

?这很简单。



映射如下所示:

  < property name =OrganizationType>< / property> 

模型属性如下所示:

  public virtual OrganizationTypes OrganizationType {get;组; 

枚举如下所示:

  public enum OrganizationTypes 
{
NonProfit = 1,
ForProfit = 2
}
/ pre>

NHibernate会自动计算出来。为什么要输入超过你需要的????


Is there a way to persist an enum to the DB using NHibernate? That is have a table of both the code and the name of each value in the enum.

I want to keep the enum without an entity, but still have a foreign key (the int representation of the enum) from all other referencing entities to the enum's table.

解决方案

Why are you guys over complicating this? It is really simple.

The mapping looks like this:

<property name="OrganizationType"></property>

The model property looks like this:

public virtual OrganizationTypes OrganizationType { get; set; }

The Enum looks like this:

public enum OrganizationTypes
{
    NonProfit = 1,
    ForProfit = 2
}

NHibernate will automatically figure it all out. Why type more than you need????

这篇关于如何使用NHibernate持久化枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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