Normalizing EnumStringType in NHibernate

*爱你&永不变心* 提交于 2019-12-22 10:49:45

问题


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>

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.

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.


回答1:


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#.

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.



来源:https://stackoverflow.com/questions/335533/normalizing-enumstringtype-in-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!