在NHibernate的正火EnumStringType [英] Normalizing EnumStringType in NHibernate

查看:198
本文介绍了在NHibernate的正火EnumStringType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用在NHibernate的枚举映射,如下所示。

I am currently using an enumeration in an NHibernate mapped as follows..

public enum UploadMethod
{
    Java, Silverlight, Gears, Flash
}

class UploadMethodType : EnumStringType
{
    public UploadMethodType() : base(typeof(UploadMethod), 255) { }
}

public class Person
{
    /* Bunch of non interesting properties... */
    public UploadMethod PreferredUploadMethod { get; set; }
}

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 <class name="Person" lazy="false" table="[dbo].[People]">
  <!-- Bunch of non interesting properties... -->
  <property name="PreferredUploadMethod" type="UploadMethodType" />
 </class>
</hibernate-mapping>



这只是数据库是远远归完美的作品,在人民表中的每一行都有一个列包含四个文本字符串之一。现在我已经分开这个到一个新表,但我不知道如何创建NHibernate的映射。我的第一反应是刚<许多一对一> ,但我不希望此枚举是其自己的实体,我基本上只需要NHibernate的做一个简单连接要钉在额外的列。

Which works perfectly except that the database is far from normalized, every row in the People table has a column containing one of four text strings. I've now separated this into a new table, but I'm not sure how to create the mapping in NHibernate. My first instinct was just <many-to-one> but I don't want this enumeration to be its own entity, I basically just need NHibernate to do a simple join to tack on the extra column.

正如我刚刚创建了一个视图,它工作正常停止的差距,但我宁愿是抽象层,在我的NHibernate的映射,不是架构。

As a stop gap I just created a view, which works fine, but I'd rather that abstraction layer be in my NHibernate mapping, not the schema.

推荐答案

如果我理解你的权利,你的数据库是完全标准化。你告诉NHibernate的,以枚举存储为一个字符串,这是一件好事。枚举不是一个实体。它的定义不应该被存储在数据库中,因为它是在C#中定义的。

If I understand you right, your database is normalized perfectly. You told NHibernate to store the Enum as a string, which is a good thing. The enum is not an entity. Its definition should not be stored in the database, because it is defined in C#.

如果您删除EnumStringType并映射枚举为的Int32,你把它作为一个数字。这可能看起来更加标准化为您,但是,当你改变你的枚举,并有应对遗留数据库的潜在问题。

If you remove the EnumStringType and map the enum as Int32, you get it as a number. This may look more "normalized" for you, but is a potential problem when you change your enum and have to cope with legacy databases.

这篇关于在NHibernate的正火EnumStringType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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