Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent NHibernate, varbinary(max) and SQLite

I have a varbinary field in my sql server database that needs to be varbinary(max). I create my database with NHibernate and I use Fluent Nhibernate for my mappings.

I also use SQLite for my unit tests, I use the same mappings I just change the configuration before creating the database in memory.

I get the following problem.

I created this extension method:

 public static IProperty WithMaxVarBinaryLength(this IProperty propertyMap)
 {
     return propertyMap.CustomSqlTypeIs("varbinary(max)");
 }

It works fine on my website, the database is created with a varbinary(max) field, but when I run my unit tests I get the following exception

System.Data.SQLite.SQLiteException: SQLite error near "max": syntax error

Then I found in another question on stackoverflow that we can do this to create a varbinary(max):

public static IProperty WithMaxLength(this IProperty propertyMap)
{
    return propertyMap.WithLengthOf(1000);
}

But I get this exception:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: Content is not a string. at FluentNHibernate.Mapping.PropertyMap.WithLengthOf(Int32 length) in d:\Builds\FluentNH\src\FluentNHibernate\Mapping\PropertyMap.cs:line 166

For the moment I am out of idea, I don't want to have to create manually all my database scripts and I want to continue using SQLite for my unit tests.

Thanks for the help.

By the way, here's my complete mapping, note that I used my extension methods.

public class AttachmentFileMap : ClassMap<AttachmentFile>
{
    public AttachmentFileMap()
    {
        WithTable("AttachmentFiles");

        Id(x => x.Id).GeneratedBy.Identity();
        Map(x => x.Content).WithMaxVarBinaryLength();
        Map(x => x.ContentType);
        Map(x => x.FileName);
        Map(x => x.ContentLength);
    }
}

Content is a byte[]

Charles

like image 341
Charles Ouellet Avatar asked Nov 15 '09 06:11

Charles Ouellet


2 Answers

Finally I found out that the new release of Fluent Nhibernate corrects this problem, you can now use .Length() after a property of byte[] type and it works perfectly.

I also had to update my Nhibernate dll and change some code in my mappings classes because the new release of Fluent Nhibernate has renamed some methods in the new release.

like image 127
Charles Ouellet Avatar answered Sep 20 '22 15:09

Charles Ouellet


You may also run into this issue when using SQL lite + NHibernate for unit testing.

The solution is to replace MAX with 2147483647

The full description can be found here: http://www.tigraine.at/2009/08/17/the-fairy-tale-of-binary-blob-fields/

like image 24
fjxx Avatar answered Sep 21 '22 15:09

fjxx