I have a very simple sqlite database with a blob column in table images for storing images, I use Nhibernate 3.2, which I installed from NuGet, and I use the new code mapping function, here is the code:
Package:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Iesi.Collections" version="3.2.0.4000" targetFramework="net40" />
<package id="NHibernate" version="3.2.0.4000" targetFramework="net40" />
</packages>
Model class:
using DBAccessLayer.DataAccess;
using NHibernate.Type;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media.Imaging;
namespace DBAccessLayer.Models
{
public class Picture
{
public virtual int Id { get; set; }
public virtual Byte[] BytesImage { get; set; }
public virtual Place Place { get; set; }
public virtual BitmapImage Image
{
get
{
return Tools.ParseBlob(BytesImage);
}
}
}
}
Mapper Class:
using DBAccessLayer.Models;
using NHibernate.Mapping.ByCode;
using NHibernate.Mapping.ByCode.Conformist;
using NHibernate.Type;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DBAccessLayer.Mappers
{
public class PictureMap : ClassMapping<Picture>
{
public PictureMap()
{
Table("images");
Id(x => x.Id, m =>
{
m.Generator(Generators.Identity);
m.Column("id");
});
Property(x => x.BytesImage, m => { m.Column("image"); });
ManyToOne(p => p.Place, map => map.Column("place_id"));
}
}
}
When I start the console with a simple request to get the image, the following nhibernate parsing error appears:
{"It is not possible to pass the value in the image2_ field of type Byte [] to type BinaryType. Please check that the mapping is set correctly and that your DataProvider supports this data type." }
{"The specified tide is invalid." }
, blob, , :
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
<property name="connection.connection_string">Data Source=datacenter.db;Version=3</property>
<property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
, , , .