English 中文(简体)
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:BuildsFluentNHsrcFluentNHibernateMappingPropertyMap.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

最佳回答

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.

问题回答

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/





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签