Persisting Blob Streams with NHibernate

2019-03-16 01:49发布

问题:

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?

回答1:

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.