Nhibernate无法反序列化可序列化的属性

本文关键字:序列化 属性 反序列化 Nhibernate | 更新日期: 2023-09-27 17:54:05

我有一个表在sql类有列一个是ID,另一个是fImage。fImage列的datatype为Image。我正在使用nhibernate来加载所有图像,并希望将其绑定到图片框控件中。但是我们在使用nhibernate读取数据时遇到了一个异常,nhibernate不能反序列化一个可序列化的属性

我在stackoverflow和google上浏览了一些链接,但似乎没有任何东西适合我。

这里我给出示例hbm文件和类文件。

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

这里是hbm文件

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

我使用的是nhibernate 3.3版本

谢谢,

Nhibernate无法反序列化可序列化的属性

将你的c#属性改为byte[]

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

你的映射不需要更多的

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

任何其他转换(到图像实例或其他)你可以在c#中做。nhibernate将byte[]正确映射到SQL Server image