简体   繁体   English

流利的NHibernate,varbinary(最大)和SQLite

[英]Fluent NHibernate, varbinary(max) and SQLite

I have a varbinary field in my sql server database that needs to be varbinary(max). 我的sql server数据库中有一个varbinary字段,需要varbinary(max)。 I create my database with NHibernate and I use Fluent Nhibernate for my mappings. 我使用NHibernate创建数据库,并使用Fluent Nhibernate作为映射。

I also use SQLite for my unit tests, I use the same mappings I just change the configuration before creating the database in memory. 我也使用SQLite进行单元测试,我使用相同的映射我只是在内存中创建数据库之前更改配置。

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 它在我的网站上工作正常,数据库是用varbinary(max)字段创建的,但是当我运行单元测试时,我得到以下异常

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): 然后我在stackoverflow的另一个问题中发现我们可以这样做来创建一个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. 目前我不知道,我不想手动创建所有数据库脚本,我想继续使用SQLite进行单元测试。

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. 最后我发现Fluent Nhibernate的新版本纠正了这个问题,你现在可以在byte []类型的属性之后使用.Length()并且它完美地工作。

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. 我还必须更新我的Nhibernate dll并更改我的映射类中的一些代码,因为Fluent Nhibernate的新版本已在新版本中重命名了一些方法。

You may also run into this issue when using SQL lite + NHibernate for unit testing. 使用SQL lite + NHibernate进行单元测试时,也可能遇到此问题。

The solution is to replace MAX with 2147483647 解决方案是用2147483647替换MAX

The full description can be found here: http://www.tigraine.at/2009/08/17/the-fairy-tale-of-binary-blob-fields/ 完整的描述可以在这里找到: http//www.tigraine.at/2009/08/17/the-fairy-tale-of-binary-blob-fields/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM