views:

468

answers:

1

Hi,

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

+1  A: 

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.

couellet