NHibernate MappingException:没有字节[]的持久化器

发布于 2024-10-28 11:47:33 字数 1431 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忘羡 2024-11-04 11:47:33

如果我没看错的话,您实际上是在尝试将 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);

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);
盛装女皇 2024-11-04 11:47:33

您指定了 BinaryBlob 的 CustomType。 NHibernate 将查找名为 BinaryBlob 的 IUserType 来执行持久性。我认为您希望 CustomSqlType 表示 MySQL 应该在数据库中使用其 BinaryBlob 类型。

public class DownloadContentMap : ClassMap<DownloadContent>
{
    public DownloadContentMap()
    {
        Id();
        Map(x => x.Data).CustomSqlType("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.

public class DownloadContentMap : ClassMap<DownloadContent>
{
    public DownloadContentMap()
    {
        Id();
        Map(x => x.Data).CustomSqlType("BinaryBlob");
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文