使用 NHibernate 持久保存 Blob 流
如果我有一个类声明为:
public class MyPersistentClass
{
public int ID { get; set; }
public Stream MyData {get;set; }
}
如何使用 NHibernate 的映射将 MyData 属性保留到数据库或从数据库保留 MyData 属性?
If I have a class declared as:
public class MyPersistentClass
{
public int ID { get; set; }
public Stream MyData {get;set; }
}
How can I use NHibernate's mappings to persist the MyData property to and from the database?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自定义类型的 Stream,并根据您的存储需求映射它。 但是正如我在 有关使用 NHibernate 延迟传输 BLOB 和 CLOB 的博客系列。
您真正需要的是一个 Blob 对象,该对象又可以创建一个 Stream 来从中读取数据。 由于 Stream 包含有关您正在读取的位置的信息,并且希望关闭和处置它,因此在域模型中直接使用时可能会产生一些问题。
我建议您看一下 博客系列以及NHibernate.Lob 项目。 它包含针对此类问题的各种映射选项。 到目前为止,几乎没有记录,但更多的信息即将到来。
You could use a Stream using a custom type and map it according to your storage needs. But there are some issues with using the Stream object as I mention in my blog series about lazy streaming of BLOBs and CLOBs with NHibernate.
What you really need is a Blob object that in turn can create a Stream to read data from. Since Stream contains information about the position you're reading from and expects to be closed and disposed of it can create some issues when used directly in a domain model.
I would suggest that you take a look at the blog series as well as the source code of the NHibernate.Lob project. It includes various mapping options for just such a problem. A little scarcely documented so far but more is coming.