How can I map a UInt64 field to a decimal(20, 0) column using Fluent nHibernate?
I'm using
- MS SQL Server 2008R2
- Fluent nHibernate 1.3
- nHibernate 3.2
I have a UInt64 field in my domain that potentially takes up the entire UInt64 range. Since MS-SQL doesn't support unsigned integers 开发者_开发技巧we decided to store it in a decimal(20,0)
column (at least for now). I've tried using CustomSqlType("decimal(20, 0)")
on that column in my mappings but I'm still getting an error when I try to access that field that says the database doesn't support UInt64.
How can I map a UInt64 field to a decimal(20, 0) column using Fluent nHibernate?
I've tried
- CustomSqlType("Decimal").Precision(20).Scale(0)
- CustomSqlType("decimal(20, 0)")
- CustomType() and the permutations of CustomSqlType and CustomType.
Edit The error it gives is "No mapping exists from DbType UInt64 to a known SqlDbType."
Edit 2 This works for the write side but breaks with an Invalid cast when values are read back out
.CustomSqlType("decimal").Precision(20).Scale(0)
.CustomType<NHibernate.Type.DoubleType>()
i would go with a user type
class UInt64UserType : ImmutableUserType
{
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var value = NHibernateUtil.Decimal.NullSafeGet(rs, names[0]);
return (value == null) ? 0 : Convert.ToUInt64(value);
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
decimal d = Convert.ToDecimal(value);
NHibernateUtil.Decimal.NullSafeSet(cmd, d, index);
}
public Type ReturnedType
{
get { return typeof(UInt64); }
}
public SqlType[] SqlTypes
{
get { return new[] { SqlTypeFactory.Decimal }; }
}
}
.CustomType<UInt64UserType>()
Edit: sorry forgot to note. i have a lot of usertypes so i implemented my own baseclasses
public abstract class ImmutableUserType : UserTypeBase
{
public override bool IsMutable
{
get { return false; }
}
public override object DeepCopy(object value)
{
return value;
}
public override object Replace(object original, object target, object owner)
{
return original;
}
public override object Assemble(object cached, object owner)
{
return cached;
}
public override object Disassemble(object value)
{
return value;
}
}
public abstract class UserTypeBase : IUserType
{
public new virtual bool Equals(object x, object y)
{
return EqualsHelper.Equals(x, y);
}
public virtual int GetHashCode(object x)
{
return (x == null) ? 0 : x.GetHashCode();
}
public abstract object Assemble(object cached, object owner);
public abstract object DeepCopy(object value);
public abstract object Disassemble(object value);
public abstract bool IsMutable { get; }
public abstract object NullSafeGet(System.Data.IDataReader rs, string[] names, object owner);
public abstract void NullSafeSet(System.Data.IDbCommand cmd, object value, int index);
public abstract object Replace(object original, object target, object owner);
public abstract Type ReturnedType { get; }
public abstract SqlType[] SqlTypes { get; }
}
精彩评论