使用byte []列进行Orchard迁移会引发NHibernate长度映射错误

时间:2013-01-03 12:13:37

标签: nhibernate c#-4.0 file-upload orchardcms

我在自定义Orchard模块中为新表创建了数据迁移。此表需要将上载的文件数据存储在其中一列中。以下是相关列的迁移代码(为简洁起见,删除了其他列):

SchemaBuilder.CreateTable("Attachment", table => table
    .Column<byte[]>("Content", col => col.WithLength(2147483647).WithType(DbType.Binary).Unlimited())
);

我尝试添加/删除WithLength和Unlimited方法,但都没有阻止错误发生。这是NHibernate的例外:

The length of the byte[] value exceeds the length configured in the mapping/parameter.
  at NHibernate.Type.AbstractBinaryType.Set(IDbCommand cmd, Object value, Int32 index)
  at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
  at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
编辑:我一直在玩添加二进制长度约定(参见此处:NHibernate Image Storage - The length of the byte[] value exceeds the length configured)之类的内容,但这看起来没有任何帮助,但看起来DbType.Binary的限制为8000字节,这是我正在打的天花板。来自MSDN docs:DbType.Binary - 可变长度的二进制数据流,范围在1到8,000字节之间。答案是使用SqlDbType.Image,它不受此限制,但Orchard Schema Builder无法使用它。

编辑2:我忘记添加 - 当我调用IRepository<T>.Create()IRepository<T>.Update()来创建或更新存储在附件表中的域对象时,会发生错误。

有没有办法覆盖/扩展SchemaBuilder,以便我可以使用SqlDbType.Image作为列类型?我正在使用Orchard 1.6

1 个答案:

答案 0 :(得分:0)

因此看起来问题源于Orchard必须同时支持SQL Server和SQL Compact,而Compact Edition不支持VARBINARY(MAX)。我能看到的唯一前进方法是在已经存在于Orchard中的东西之上创建我自己的架构构建器。我将采用的方法是创建一个自定义CreateColumnCommand和一个自定义DataMigrationInterpreter(以及我需要的任何其他相关的东西)来为我执行模式生成。一旦它工作,我会发布相关的代码。如果有人认为这种方法存在缺陷,请大声呼喊!

编辑:但是,仔细观察后,SQL Server CE支持Image类型,所以我仍然不知道为什么Orchard无法做到这一点。如果没有替代方案,我会继续关注我的自定义解决方案。