我有一个非常简单的 sqlite 数据库,在表图像中有一个 blob 列用于存储图像,我使用的是从 NuGet 安装的 Nhibernate 3.2,我正在使用按代码映射的新功能,代码如下:
包裹:
<?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>
模型类:
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);
}
}
}
}
映射器类:
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"));
}
}
}
当我在控制台上运行一个简单的查询来检索图片时,出现下一个 nhibernate 解析错误:
{“无法将类型为 Byte[] 的字段 image2_ 中的值转换为 BinaryType 类型。请检查以确保映射正确并且您的 DataProvider 支持此数据类型。”
{“指定的转换无效。”}
我正在使用这个配置并且它在没有映射 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>
我需要解决这个问题,这样我才能检索存储在我的数据库中的图像,感谢任何帮助。
最佳答案
试试这个博客:
- http://blog.calyptus.eu/seb/2009/03/large-object-storage-for-nhibernate-and-ddd-part-1-blobs-clobs-and-xlobs/
- http://kozmic.net/2009/08/13/adjusting-nhibernate-mapping-for-tests/
- http://nhibernate.info/blog/
他们应该在这里解决它的问题。
关于c# - 使用字节 [] 的 Nhibernate 3.2 sqlite blob 字段通过代码映射不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22723301/