简体   繁体   English

使用枚举映射NHibernate 3类时出错

[英]Error when mapping an NHibernate 3 class with enums

So I am getting an error with my mapping with my xml file in NHibernate 3.0. 因此,我在NHibernate 3.0中使用xml文件映射时遇到错误。 I have a MediaContent class that I am trying to map out and am having issues with it. 我有一个MediaContent类,我正在尝试绘制该类并遇到问题。 I know it has something to do with my xml mapping because the mapping isn't showing up in my configuration. 我知道它与xml映射有关,因为该映射未显示在我的配置中。

Here is the MediaContent class (The enums are ContentAccessibility and MediaTypes): 这是MediaContent类(枚举是ContentAccessibility和MediaTypes):

public class MediaContent:IMediaContent,ICreatedAndModified, IActive, IArchived
{
    #region Properties/Members
    public int MediaContentID { get; set; }
    public string Name { get; set; }
    public string FileName { get; set; }
    public ContentAccessibility Accessibility { get; set; }
    public MediaTypes MediaType { get; set; }
    public object Media { get; set; }
    public Dictionary<string, object> MediaProperties { get; set; }
    public string Author { get; set; }
    public string InternalIdentifier { get; set; }
    public string CreatedBy { get; private set; }
    public DateTime? CreatedOn { get; private set; }
    public string LastModifiedBy { get; private set; }
    public DateTime? LastModifiedOn { get; private set; }
    public string ComplianceCode { get; private set; }

    public bool IsActive { get; private set; }
    public bool Archived { get; private set; }
    #endregion


    #region Methods
    public void Create(DateTime createdOn, string createdBy)
    {
        CreatedOn = createdOn;
        CreatedBy = createdBy;
    }
    public void Modified(DateTime modifiedOn, string modifiedBy)
    {
        LastModifiedBy = modifiedBy;
        LastModifiedOn = modifiedOn;
    }
    #endregion


}

Here is the MediaContent.hbm.xml file: 这是MediaContent.hbm.xml文件:

    <?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                                     assembly="Domain"
                                     namespace="Domain.Models">

  <class name="MediaContent" lazy="false" table="MediaContent">
    <!-- Identity Mapping-->
    <id name="MediaContentID">
      <column name="MediaContentID" />
      <generator class="native" />
    </id>

    <!-- Simple Property Mappings-->    
    <property name="Name" />
    <property name="FileName" />
    <property name="Accessibility" />
    <property name="MediaType" />
    <property name="Media" />
    <property name="Author" />
    <property name="InternalIdentifier" />
    <property name="ComplianceCode" />
    <property name="CreatedBy" />
    <property name="CreatedOn" />
    <property name="LastModifiedBy" />
    <property name="LastModifiedOn" />    
    <property name="IsActive" />
    <property name="Archived" />  

  </class>
</hibernate-mapping>

It could be that I am overlooking how things work with NHibernate, but as far as I know things should be fine otherwise. 可能是我忽略了NHibernate的工作方式,但是据我所知,否则应该没问题。

You have a myriad of problems here: 您这里有许多问题:

  1. It looks like MediaType and ContentAccessibility are concrete classes so you need some kind of reference mapping. 看起来MediaType和ContentAccessibility是具体的类,因此您需要某种类型的引用映射。 They are not properties, as this is typically reserved for primitive properties like strings, ints, dates, etc. 它们不是属性,因为通常保留给诸如字符串,整数,日期等原始属性。

  2. At least one of the properties is an object type, which you can't simply map as a property. 属性中至少有一个是对象类型,您不能简单地将其映射为属性。 I had this situation come up fairly recently and found it was easier to map it as a string and then convert it to the proper thing (guid, date, etc) on the domain. 我最近刚遇到这种情况,发现将它映射为字符串然后将其转换为域上的正确内容(GUID,日期等)更加容易。

I don't remember HBM perfectly but you may also need something to indicate that DateTime properties can be nullable. 我不太记得HBM,但您可能还需要一些信息来指示DateTime属性可以为空。 Were I you, I would look into NH mappings by code. 如果是我,我会通过代码研究NH映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM