【发布时间】:2015-04-10 09:26:11
【问题描述】:
我正在尝试从 SQL Server 数据库中读取小数点 (38,16) 并苦苦挣扎。经过大量阅读后,我尝试使用以下代码为 SQL Decimal 实现自定义类型:
public class BigDecimal : IUserType
{
public bool Equals(object x, object y)
{
return object.Equals(x,y);
}
public int GetHashCode(object x)
{
return x.GetHashCode();
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
int index = rs.GetOrdinal(names[0]);
object result = rs.GetValue(index);
return result;
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
//Not got here yet
}
public object DeepCopy(object value)
{
return value;
}
public object Replace(object original, object target, object owner)
{
return original;
}
public object Assemble(object cached, object owner)
{
return cached;
}
public object Disassemble(object value)
{
return value;
}
public SqlType[] SqlTypes { get { return new[] {SqlTypeFactory.Decimal}; } }
public Type ReturnedType { get { return typeof (SqlDecimal); } }
public bool IsMutable { get { return false; } }
}
但是 rs.GetValue 的输出是一个小数,而不是 SQLDecimal,这会导致溢出异常。
类如下所示:
public class Billy
{
public BigDecimal TheNumber {get;set;}
}
映射如下所示:
public class BillyMap : ClassMap<Billy>
{
public BillyMap()
{
Map(b=>b.TheNumber).CustomType<BigDecimal>();
}
}
请有人告诉我哪里出错了。
【问题讨论】:
标签: nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping custom-type