Search code examples
c#sql-server-2008filestreamcastle-activerecord

How to use Castle ActiveRecord with SQL Server 2008 FILESTREAM feature


I would like to save images (C#) to the database by using the FILESTREAM feature from SQL SERVER 2008.

I've configured and enabled FILESTREAM in SQL Server 2008 without any problem.

How do I use this new feature from SQL Server through Castle ActiveRecord to save images to the database.

Is there a property attribute that I have to add in order to tell castle activerecord to use FILESTREAM ?

Suppose I have the following class:

[ActiveRecord]
public class Picture : ActiveRecordBase
{
   [PrimaryKey]
   public int Id { get; set; }

   [Property]
   public byte[] PictureData { get; set; }
}

Is this sufficient to use the FILESTREAM feature from SQL SERVER? I haven't found much example using Castle ActiveRecord(NHibernate) with FILESTREAM.

Thanks!

Edit: I'm not sure what's going on. It seems like whatever I do, the image data is saved directly into the database instead of using the NTFS file system as mentionned in the SQL SERVER 2008 FileStream documentation.

The above PictureData property now looks like this:

[Property(SqlType = "VARBINARY(MAX)"]
public byte[] PictureData { get; set; }

Edit:

I found out that Castle ActiveRecord creates the PictureData field as a varbinary(max) type but it should be VARBINARY(MAX) FILESTREAM

How do you tell Castle ActiveRecord to create such a field?


Solution

  • Well I found the answer to my own question.

    This is how you need to define your Castle ActiveRecord properties to enable FileStream.

    [Property(Unique = true, NotNull = true, SqlType = "UNIQUEIDENTIFIER ROWGUIDCOL", Default = "(newid())")]
    public Guid ImageGuid { get; set; }
    
    [Property(SqlType = "VARBINARY(MAX) FILESTREAM")]
    public byte[] ImageFile { get; set; }
    

    This should help some people out there. Not many info about this!!