NHibernate Mapping文件中如何指定类的字节数组属性
对于字节数组类型的属性映射,可以用Byte[]指定其Type,但是这中类型只能保存8000个字节(虽然你可以指定超过8000的Length属性,而且生成的表字段类型也为Image)。 如果要保存任意长的字节数据,需要用到BinaryBlob类型。
举个例子,如果Employee类有一Photo属性为字节数组:
public class Employee
{
//其他略去
public byte[] Photo
{
get
{
return _photo;
}
set
{
_photo = value;
}
}
}
{
//其他略去
public byte[] Photo
{
get
{
return _photo;
}
set
{
_photo = value;
}
}
}
在影射文件中可以用 BinaryBlob 类型:
<class name="Employee" table="[Employee]">
<id name="ID" column="EmployeeID" unsaved-value="0">
<generator class="native" />
</id>
<!--其他略去-->
<property name="Photo" column="[Photo]" not-null="false" type="BinaryBlob"/> </class>
<id name="ID" column="EmployeeID" unsaved-value="0">
<generator class="native" />
</id>
<!--其他略去-->
<property name="Photo" column="[Photo]" not-null="false" type="BinaryBlob"/> </class>
另外,其他大对象的映射可参考下表:
NHibernate Type | .NET Type | Database Type | Remarks |
---|---|---|---|
StringClob |
System.String |
DbType.String |
type="StringClob" must be specified. Entire field is read into memory. |
BinaryBlob |
System.Byte[] |
DbType.Binary |
type="BinaryBlob" must be specified. Entire field is read into memory. |
Serializable |
Any System.Object that is marked with SerializableAttribute. |
DbType.Binary |
type="Serializable" should be specified. This is the fallback type if no NHibernate Type can be found for the Property. |
字节数组可以持久化之后,自然会担心内存占用问题。那么能不能对属性也做Lazy Initialization的实现呢?这可能是NHibernate要面对的新需求。