c# - 流利的 NHibernate、varbinary(max) 和 SQLite

标签 c# .net nhibernate fluent-nhibernate mapping

我的 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);
}

但是我得到了这个异常(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

目前我不知道,我不想手动创建所有数据库脚本,我想继续使用 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);
    }
}

内容是一个byte[]

查尔斯

最佳答案

最后我发现新版本的 Fluent Nhibernate 纠正了这个问题,你现在可以在 byte[] 类型的属性后使用 .Length() 并且它工作得很好。

我还必须更新我的 Nhibernate dll 并更改我的映射类中的一些代码,因为 Fluent Nhibernate 的新版本重命名了新版本中的一些方法。

关于c# - 流利的 NHibernate、varbinary(max) 和 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1736754/

相关文章:

c# - SQL SERVER 删除大量行优化

c# - 选择 GoF 模式

c# - 使用 API 时从 MSBUILD 中跳过/排除项目类型

c# - 如何按属性删除 XML 节点

performance - 读取大包时 NHibernate 变慢

NHibernate:用于检索具有空计数子集合的所有实体的条件表达式

c# - 在 ASP.NET MVC 中通过 CancellationToken 中止 AJAX 请求

c# - 任务完成后更改文本框值

.NET 绘制全屏游戏

c# - 如何使用 C# 匹配正则表达式中的 "