C# NHibernate映射异常:字节[]没有持久化程序
我正在使用NHibernate在我的MySQL数据库中为ASP.NETMVC网站存储下载内容。我习惯于上课。一个名为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;
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)代码>