简体   繁体   中英

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. I have a MediaContent class that I am trying to map out and am having issues with it. I know it has something to do with my xml mapping because the mapping isn't showing up in my configuration.

Here is the MediaContent class (The enums are ContentAccessibility and 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:

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

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

I don't remember HBM perfectly but you may also need something to indicate that DateTime properties can be nullable. Were I you, I would look into NH mappings by code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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