NHibernate的MappingException:没有持留字节[] [英] NHibernate MappingException: no persister for byte[]

查看:265
本文介绍了NHibernate的MappingException:没有持留字节[]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NHibernate来存储下载我的一个ASP.NET MVC的网站的MySQL数据库。我使用的类。一位名为下载的下载本身和一个名为 DownloadContent 的文件本身(这样我就可以更容易当我加载只是想获得元数据)。

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);

我得到一个 NHibernate.MappingException 消息不持留为:System.Byte []。我看着它的NHibernate的文档和byte []应该正确映射。

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.

我是什么做错了吗?

推荐答案

如果我读正确你实际上是试图保存字节[] 到数据库,这不能工作,因为字节[] 不是一个映射的实体。

If I read that correctly you are actually trying to save the byte[] to the DB, which can't work, since byte[] is not a mapped entity.

您可能希望这样写:

dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(dl); // content is wrong, since content is of type byte[]

此外,因为你没有指定逆(),你可能会为 SaveOrUpdate DownloadContent 第一,因此:

Also, since you didn't specify an Inverse(), you will probably have to SaveOrUpdate the DownloadContent first, therefore:

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);

这篇关于NHibernate的MappingException:没有持留字节[]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆