【发布时间】:2011-07-01 10:12:23
【问题描述】:
我有一个包含 XmlElement 类型的属性的类。 属性映射如下:
<property name="XamlForm" column="XamlForm" type="KTN.Base.Data.Types.XmlType, KTN.Base.Data" />
XmlType 类:
[Serializable]
public class XmlType : IUserType
{
public new bool Equals(object x, object y)
{
XmlElement xdoc_x = (XmlElement)x;
XmlElement xdoc_y = (XmlElement)y;
if (xdoc_x == null && xdoc_y == null) return true;
if (xdoc_x == null || xdoc_y == null) return false;
return xdoc_y.OuterXml == xdoc_x.OuterXml;
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
if (names.Length != 1)
throw new InvalidOperationException("names array has more than one element. can't handle this!");
XmlDocument document = new XmlDocument();
string val = rs[names[0]] as string;
if (val != null)
{
document.LoadXml(val);
return document.DocumentElement;
}
return null;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
DbParameter parameter = (DbParameter)cmd.Parameters[index];
if (value == null)
{
parameter.Value = DBNull.Value;
return;
}
parameter.Value = ((XmlElement)value).OuterXml;
}
public object DeepCopy(object value)
{
if (value == null) return null;
XmlElement other = (XmlElement)value;
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(other.OuterXml);
return xdoc.DocumentElement; /**/
}
public SqlType[] SqlTypes
{
get
{
return new SqlType[] { new SqlXmlType() };
}
}
public System.Type ReturnedType
{
get { return typeof(XmlDocument); }
}
public bool IsMutable
{
get { return true; }
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object Replace(object original, object target, object owner)
{
// Changed return original to return target...
return target;
}
}
从 NHibernate 调用 Merge 函数后,该属性为空。 任何的想法? 提前致谢
【问题讨论】:
-
您可能希望在 Equals fn 中进行空检查
标签: nhibernate merge xmltype