Sql server type FileStream in nhibernate

I would like to use FileStream with Nhibernate.

The only post I found on stackoverflow, Sql 2008 Filestream with NHibernate

Meanwhile, Nhibernate 3.0 (and 3.1) was released.

Does anyone know a solution?

+3
source share
2 answers

It is planned that nhibernate will not be supported. (WONT FIX)

http://groups.google.com/group/nhusers/browse_thread/thread/e4a8f038f9c40a0d/a63ac4a8ce4f1244?pli=1

I hope this changes in the future ... maybe an extension (open source magic)

+2
source

Nhibernate FILESTREAM... . , Nhibernate, . SQL-, . .

public void Save(Photo photo){
            //Save photo data
            session.Save(photo);
            //get path
            String path;
            Byte[] context;
            IQuery qry1 = session.GetNamedQuery(QUERY_SET_BLANK);
            qry1.SetInt64("photoId", photo.RID);
            int cnt = qry1.ExecuteUpdate();
            IQuery qry2 = session.GetNamedQuery(QUERY_GET_PATH);
            qry2.SetInt64("photoId", photo.RID);
            System.Collections.IList results = qry2.List();
            object[] item = (object[]) results[0];
            path = (String) item[0];
            context = (Byte[])item[1];
            if (context == null) throw new QueryException("Possible null transaction");
            //save photo
            using (SqlFileStream sqlFile = new SqlFileStream(path, context, FileAccess.Write)) {
                photo.Image.Save(sqlFile, ImageFormat.Jpeg);
                sqlFile.Close();
            }
        }

public Photo Get(Int64 rid) {
            Photo result = session.Get<Photo>(rid);
            if (result != null) {
                IQuery qry = session.GetNamedQuery(QUERY_GET_PATH);
                qry.SetInt64("photoId", result.RID);
                System.Collections.IList results = qry.List();
                object[] item = (object[])results[0];
                var path = (String)item[0];
                var context = (Byte[])item[1];
                if (context == null) throw new QueryException("Possible null transaction");
                using (SqlFileStream sqlFile = new SqlFileStream(path, context, FileAccess.Read))
                    result.Image = new System.Drawing.Bitmap(System.Drawing.Bitmap.FromStream(sqlFile));
            }
            return result;
        }

- , . FILESTREAM ( ), . :

<sql-query name="PhotoPathContext">
    select Photo.PathName() as path, GET_FILESTREAM_TRANSACTION_CONTEXT() as con 
      from Core.Photos where PhotoRID = :photoId
  </sql-query>
  <sql-query name="PhotoSetBlankFileStream">
    update Core.Photos set Photo = Cast('' as varbinary(max)) where PhotoRID = :photoId
  </sql-query>

, GET_FILESTRAM_TRANSACTION_CONTEXT() .

:

[Test]
public void TestSavePhoto() {
    IList<Model.Photo> photos = repo.GetPhotos();
    VegTabUtilityServices.Photo photo = new VegTabUtilityServices.Photo();
    VegTabUtil.Model.Photo ph = photos[0];
    photo.RowGuid = ph.GetGuid().Value;
    photo.Name = ph.Name ?? photo.RowGuid.ToString();
    photo.Image = ph.Image;
    ISession session = SessionManager.Instance.Session;
    PhotoService ps = new PhotoService(session);
    using (NHibernate.ITransaction tx = session.BeginTransaction()) {
        ps.Save(photo);
        tx.Commit();
    }
    Assert.Greater(photo.RID, 0);
}

[Test]
public void TestPhotoConnection() {
    ISession session = SessionManager.Instance.Session;
    PhotoService ps = new PhotoService(session);
    Photo p;
    using (NHibernate.ITransaction tx = session.BeginTransaction()) {
        p = ps.Get(6l);
        tx.Commit();
    }
    Assert.NotNull(p);
    Assert.NotNull(p.Image);
    logger.Debug(String.Format("{0} by {1} pixels", p.Image.Width, p.Image.Height));
}

codeproject

0

Source: https://habr.com/ru/post/1708393/


All Articles