9

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

2 Answers 2

5

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/

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.