开发者

NHibernate MappingException: no persister for byte[]

开发者 https://www.devze.com 2023-02-20 21:44 出处:网络
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

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 Downlo开发者_StackOverflowadContentMap()
    {
        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?


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.

You probably want to write:

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

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


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.

public class DownloadContentMap : ClassMap<DownloadContent>
{
    public DownloadContentMap()
    {
        Id();
        Map(x => x.Data).CustomSqlType("BinaryBlob");
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消