Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# NHibernate映射异常:字节[]没有持久化程序_C#_.net_Mysql_Nhibernate_Fluent Nhibernate - Fatal编程技术网

C# NHibernate映射异常:字节[]没有持久化程序

C# NHibernate映射异常:字节[]没有持久化程序,c#,.net,mysql,nhibernate,fluent-nhibernate,C#,.net,Mysql,Nhibernate,Fluent Nhibernate,我正在使用NHibernate在我的MySQL数据库中为ASP.NETMVC网站存储下载内容。我习惯于上课。一个名为Download用于下载本身,另一个名为DownloadContent用于文件本身(因此,当我只想获取元数据时,可以更轻松地加载) 数据类声明和映射如下所示: public class Download { public virtual string Id { get; set; } public virtual string OutFileName { get;

我正在使用NHibernate在我的MySQL数据库中为ASP.NETMVC网站存储下载内容。我习惯于上课。一个名为
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文档查找它,字节[]应该正确映射


我做错了什么?

如果我读对了,您实际上是在试图将
字节[]
保存到数据库中,该数据库无法工作,因为
字节[]
不是映射实体

你可能想写:

dl.Contents = new DownloadContent { Data = content };
db.session.SaveOrUpdate(dl); // content is wrong, since content is of type byte[]
另外,由于您没有指定
Inverse()
,您可能必须先
保存或更新
下载内容,因此:

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");
    }
}
public类下载contentmap:ClassMap
{
公开下载ContentMap()
{
Id();
Map(x=>x.Data);
}
}

@DontCare4Free您的代码显示
db.session.SaveOrUpdate(内容)
除非是打字错误或我遗漏了什么,“content”是byte[]类型的,您不能将其作为参数传递给
SaveOrUpdate()
它是围绕byte[]类型的包装器。@DontCare4Free wrapper与否,错误
No persister for:System.byte[]
肯定指向了这一事实,
SaveOrUpdate()
中的参数类型为byte[]。请尝试将该行替换为
db.session.SaveOrUpdate(dl)