【问题标题】:How can I map a UInt64 field to a decimal(20, 0) column using Fluent nHibernate?如何使用 Fluent nHibernate 将 UInt64 字段映射到 decimal(20, 0) 列?
【发布时间】:2011-11-20 10:23:18
【问题描述】:

我正在使用

  • MS SQL Server 2008R2
  • 流畅的 nHibernate 1.3
  • nHibernate 3.2

我的域中有一个 UInt64 字段,它可能占用整个 UInt64 范围。由于 MS-SQL 不支持无符号整数,我们决定将其存储在 decimal(20,0) 列中(至少现在是这样)。我已经尝试在我的映射中的该列上使用CustomSqlType("decimal(20, 0)"),但是当我尝试访问该数据库不支持 UInt64 的字段时仍然出现错误。

如何使用 Fluent nHibernate 将 UInt64 字段映射到 decimal(20, 0) 列?

我试过了

  • CustomSqlType("Decimal").Precision(20).Scale(0)
  • CustomSqlType("decimal(20, 0)")
  • 自定义类型() 以及 CustomSqlType 和 CustomType 的排列。

编辑 它给出的错误是“不存在从 DbType UInt64 到已知 SqlDbType 的映射。”

编辑 2 这适用于写入端,但在读回值时会因 Invalid cast 而中断

.CustomSqlType("decimal").Precision(20).Scale(0)
.CustomType<NHibernate.Type.DoubleType>()

【问题讨论】:

    标签: orm fluent-nhibernate nhibernate-mapping sql-server-2008-r2


    【解决方案1】:

    我会选择用户类型

    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>()
    

    编辑:抱歉忘记注意了。我有很多用户类型,所以我实现了自己的基类

    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; }
    }
    

    【讨论】:

    • ImmutableUserType 在什么 dll 或命名空间?我在 NHibernate.UserTypes 中没有看到它,而且我的 Google 技能让我失望了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    相关资源
    最近更新 更多