NHibernate MappingException:没有字节[]的持久化器
我正在使用 NHibernate 将下载内容存储在 ASP.NET MVC 网站的 MySQL 数据库中。我正在习惯上课。一个名为 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
,其中包含消息“No persister for: System.Byte[]”。我用 NHibernate 文档查找了它,并且 byte[] 应该正确映射。
我做错了什么?
I'm using NHibernate to store downloads in my MySQL database for an ASP.NET MVC website. I am using to classes. One called Download
for the download itself and one called DownloadContent
for the file itself (so I can load it easier when I just want to get the metadata).
The data class declarations and mappings look like this:
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");
}
}
Now, when I try to do like this:
dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(content);
I get an NHibernate.MappingException
with the message "No persister for: System.Byte[]". I looked it up with the NHibernate docs and byte[] should map correctly.
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我没看错的话,您实际上是在尝试将 byte[] 保存到数据库中,这是行不通的,因为 byte[] 不是映射实体。
您可能想写:
另外,由于您没有指定
Inverse()
,您可能必须首先SaveOrUpdate
DownloadContent
,所以:If I read that correctly you are actually trying to save the
byte[]
to the DB, which can't work, sincebyte[]
is not a mapped entity.You probably want to write:
Also, since you didn't specify an
Inverse()
, you will probably have toSaveOrUpdate
theDownloadContent
first, therefore:您指定了 BinaryBlob 的 CustomType。 NHibernate 将查找名为 BinaryBlob 的 IUserType 来执行持久性。我认为您希望 CustomSqlType 表示 MySQL 应该在数据库中使用其 BinaryBlob 类型。
You specified a CustomType of BinaryBlob. NHibernate will look for an IUserType called BinaryBlob to perform the persistence. I think you want CustomSqlType to say that MySQL should use its BinaryBlob type in the database.