[英]NHibernate MappingException: no persister for byte[]
我正在使用NHibernate将下载的内容存储在我的MySQL数据库中,用于ASP.NET MVC网站。 我正在上课。 一个称为Download
本身的下载,另一个称为文件本身的DownloadContent
(因此当我只想获取元数据时,可以更轻松地加载它)。
数据类的声明和映射如下所示:
public class Download
{
public virtual string Id { get; set; }
public virtual string OutFileName { get; set; }
public virtual DownloadContent Contents { get; set; }
public virtual string MimeType { get; set; }
public virtual bool DoForward { get; set; }
public virtual string RedirectLink { get; set; }
}
public class DownloadMap : ClassMap<Download>
{
public DownloadMap()
{
Id(x => x.Id);
Map(x => x.OutFileName);
References<DownloadContent>(x => x.Contents);
Map(x => x.MimeType);
Map(x => x.DoForward).Not.Nullable();
Map(x => x.RedirectLink);
}
}
public class DownloadContent
{
public virtual byte[] Data { get; set; }
}
public class DownloadContentMap : ClassMap<DownloadContent>
{
public DownloadContentMap()
{
Id();
Map(x => x.Data).CustomType("BinaryBlob");
}
}
现在,当我尝试这样做时:
dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(content);
我收到一个NHibernate.MappingException
消息,消息为“ System.Byte []没有持久性”。 我使用NHibernate文档进行了查找,并且byte []应该正确映射。
我究竟做错了什么?
如果我没看错,您实际上是在尝试将byte[]
保存到数据库中,这是行不通的,因为byte[]
不是映射的实体。
您可能想写:
dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(dl); // content is wrong, since content is of type byte[]
另外,由于您没有指定Inverse()
,因此可能必须先SaveOrUpdate
或更新DownloadContent
,因此:
Download dl = new Download { OutFileName = "Test", DoForward = true };
DownloadContent dlc = new DownloadContent { Data = content };
dl.Contents = dlc;
db.session.SaveOrUpdate(dlc);
db.session.SaveOrUpdate(dl);
您指定了BinaryBlob的CustomType。 NHibernate将寻找一个名为BinaryBlob的IUserType来执行持久性。 我认为您希望CustomSqlType表示MySQL应该在数据库中使用其BinaryBlob类型。
public class DownloadContentMap : ClassMap<DownloadContent>
{
public DownloadContentMap()
{
Id();
Map(x => x.Data).CustomSqlType("BinaryBlob");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.