Search code examples
c#nhibernatenhibernate-mapping

Nhibernate could not deserialize a serializable property


I have a table in sql class which have to columns one is ID and another one is fImage. datatype of fImage column is Image. I am using nhibernate to load all images and want to bind it into picture box control. But we are getting exception when reading data using nhibernate that nhibernate could not deserialize a serializable property .

I went through some links on stackoverflow and google but nothing seems to be work for me.

Here I am giving sample hbm file and class file.

namespace BlackOpsP2.Core.Domain.ARModule
{
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

/// <summary>
/// Documentation for the tARReportLogo.
/// </summary>
public partial class tARReportLogo : DomainObject<System.Guid>
{
    #region Constructor
    /// <summary>
    /// Initializes a new instance of the <see cref="tARReportLogo"/> class.
    /// </summary>
    public tARReportLogo()
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="tARReportLogo"/> class.
    /// </summary>
    /// <param name="fReportLogoID">The Payment Type ID.</param>
    public tARReportLogo(System.Guid fReportLogoID)
    {
        this.ID = fReportLogoID;
    }

    #endregion        

    #region Properties

    /// <summary>
    /// Gets or sets ReportLogoID.
    /// </summary> 
    public virtual System.Guid fReportLogoID { get; set; }

    /// <summary>
    /// Gets or sets Image ID.
    /// </summary> 
    public virtual System.Guid fImageID { get; set; }

    /// <summary>
    /// Gets or sets Image Name.
    /// </summary> 
    public virtual string fImageName { get; set; }

    /// <summary>
    /// Gets or sets Image Value.
    /// </summary> 
    public virtual Image fImageValue { get; set; }                

    #endregion

    #region Methods
    /// <summary>
    /// Joins a first name and a last name together into a single string.
    /// </summary>    
    /// <returns>The hash code.</returns>
    public override int GetHashCode()
    {
        return ID.GetHashCode();
    }
    #endregion
}
}

And here is hbm file

<?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping assembly="BlackOpsP2.Core" namespace="BlackOpsP2.Core.Domain.ARModule" xmlns="urn:nhibernate-mapping-2.2">
  <class name="tARReportLogo" table="tARReportLogo" lazy="true" >
     <id name="fReportLogoID">
        <generator class="guid" />
     </id>    
     <property name="fImageID">
     </property>
     <property name="fImageName" >     
     </property>
     <property name="fImageValue" type="Serializable" length="2147483647">
     </property>    
  </class>
</hibernate-mapping>

I am using nhibernate version 3.3.

Thanks,


Solution

  • Change your C# property to byte[]

    public partial class tARReportLogo : DomainObject<System.Guid>
    {
      ...
      public virtual byte[] fImageValue { get; set; }  // image as a byte array
      ...
    }
    

    And your mapping doesn't need more then this

    <property name="fImageValue" length="2147483647" />
    

    Any other conversions (to Image instance or whatever) you can do in C#. NHiberante will correclty map byte[] into SQL Server image