C# 将对象[]存储为Fluent NHibernate中的列
我有以下(部分)模型: 我想将C# 将对象[]存储为Fluent NHibernate中的列,c#,serialization,fluent-nhibernate,fluent-nhibernate-mapping,C#,Serialization,Fluent Nhibernate,Fluent Nhibernate Mapping,我有以下(部分)模型: 我想将args[]存储为表中的一个列,提前考虑到格式参数通常是可序列化的,或者可以事先转换为字符串 我不想存储格式化的消息,我想分别存储format和args(这有几个优点) 如何告诉Fluent NHibernate使用BLOB类型存储该列,并在持久化实体时执行简单的二进制序列化/反序列化?Map(x=>x.Args).CustomType(); Map(x => x.Args).CustomType<ObjectArrayType>(); clas
args[]
存储为表中的一个列,提前考虑到格式参数通常是可序列化的,或者可以事先转换为字符串
我不想存储格式化的消息,我想分别存储format和args(这有几个优点)
如何告诉Fluent NHibernate使用BLOB类型存储该列,并在持久化实体时执行简单的二进制序列化/反序列化?Map(x=>x.Args).CustomType();
Map(x => x.Args).CustomType<ObjectArrayType>();
class ObjectArrayType : IUserType
{
public object NullSafeGet(IDBReader reader, string[] names, object owner)
{
byte[] bytes = NHibernateUtil.BinaryBlob.NullSafeGet(reader, names[0]);
return Deserialize(bytes);
}
public void NullSafeSet(IDBCommand cmd, object value, int index)
{
var args = (object[])value;
NHibernateUtil.BinaryBlob.NullSafeSet(cmd, Serialize(args), index);
}
public Type ReturnType
{
get { return typeof(object[]); }
}
public SqlType[] SqlTypes
{
get { return new [] { SqlTypeFactory.BinaryBlob } }
}
}
类ObjectArrayType:IUserType
{
公共对象NullSafeGet(IDBReader读取器,字符串[]名称,对象所有者)
{
byte[]bytes=NHibernateUtil.BinaryBlob.NullSafeGet(读取器,名称[0]);
返回反序列化(字节);
}
public void NullSafeSet(IDBCommand cmd,对象值,int索引)
{
变量args=(对象[])值;
NHibernateUtil.BinaryBlob.NullSafeSet(cmd,Serialize(args),index);
}
公共类型返回类型
{
获取{return typeof(object[]);}
}
公共SqlType[]SqlTypes
{
获取{returnnew[]{SqlTypeFactory.BinaryBlob}
}
}
您是否尝试过此问题中的建议:
Map(x => x.Args).CustomType<ObjectArrayType>();
class ObjectArrayType : IUserType
{
public object NullSafeGet(IDBReader reader, string[] names, object owner)
{
byte[] bytes = NHibernateUtil.BinaryBlob.NullSafeGet(reader, names[0]);
return Deserialize(bytes);
}
public void NullSafeSet(IDBCommand cmd, object value, int index)
{
var args = (object[])value;
NHibernateUtil.BinaryBlob.NullSafeSet(cmd, Serialize(args), index);
}
public Type ReturnType
{
get { return typeof(object[]); }
}
public SqlType[] SqlTypes
{
get { return new [] { SqlTypeFactory.BinaryBlob } }
}
}