System.Drawing.Image的Nhibernate映射

Ste*_*ger 4 c# sql vb.net nhibernate nhibernate-mapping

问题:我得到一个异常,将此类序列化为nHibernate xml文件({"无法确定类型:System.Drawing.Image,System.Drawing,列:NHibernate.Mapping.Column(Settings)"}).

如何将System.Drawing.Image映射到nHibernate?什么MS-SQL dbtype将被使用?

using System;
using System.Collections.Generic;
using System.Text;

namespace nhDBapi.Tables
{

    [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsSettings, nhDBapi", Table = "lsSettings")]
    public class clsSettings
    {

        [NHibernate.Mapping.Attributes.Id(Name = "Settings", Column = "Settings", TypeType = typeof(System.Drawing.Image))]
        public System.Drawing.Image Settings;

    } // End partial class lsSettings


} // End Namespace nhDBapi.Tables
Run Code Online (Sandbox Code Playgroud)

Mau*_*fer 9

I wouldn't recommend mapping directly to System.Drawing.Image. Not only is it disposable (NHibernate would have to dispose it and I'm not sure it can), but also if you fetch a collection of clsSettings you'll be creating lots of Image instances, thus wasting CPU and memory if you don't use all of them.

Instead, map to a byte[] with sql type varbinary and you handle converting from and to Image as necessary. Example.

Also worth checking out is this project about large object storage support for NHibernate, seems to be more efficient than mapping to a byte[] and it's also an excellent article about all options.