[英]Fluent NHibernate, varbinary(max) and SQLite
我的sql server數據庫中有一個varbinary字段,需要varbinary(max)。 我使用NHibernate創建數據庫,並使用Fluent Nhibernate作為映射。
我也使用SQLite進行單元測試,我使用相同的映射我只是在內存中創建數據庫之前更改配置。
我遇到以下問題。
我創建了這個擴展方法:
public static IProperty WithMaxVarBinaryLength(this IProperty propertyMap)
{
return propertyMap.CustomSqlTypeIs("varbinary(max)");
}
它在我的網站上工作正常,數據庫是用varbinary(max)字段創建的,但是當我運行單元測試時,我得到以下異常
System.Data.SQLite.SQLiteException: SQLite error near "max": syntax error
然后我在stackoverflow的另一個問題中發現我們可以這樣做來創建一個varbinary(max):
public static IProperty WithMaxLength(this IProperty propertyMap)
{
return propertyMap.WithLengthOf(1000);
}
但是我得到了這個例外:
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
目前我不知道,我不想手動創建所有數據庫腳本,我想繼續使用SQLite進行單元測試。
謝謝您的幫助。
順便說一句,這是我的完整映射,請注意我使用了我的擴展方法。
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);
}
}
內容是一個字節[]
查爾斯
最后我發現Fluent Nhibernate的新版本糾正了這個問題,你現在可以在byte []類型的屬性之后使用.Length()並且它完美地工作。
我還必須更新我的Nhibernate dll並更改我的映射類中的一些代碼,因為Fluent Nhibernate的新版本已在新版本中重命名了一些方法。
使用SQL lite + NHibernate進行單元測試時,也可能遇到此問題。
解決方案是用2147483647替換MAX
完整的描述可以在這里找到: http : //www.tigraine.at/2009/08/17/the-fairy-tale-of-binary-blob-fields/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.