首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Fluent UInt64将UInt64字段映射到十进制(20,0)列?

如何使用Fluent UInt64将UInt64字段映射到十进制(20,0)列?
EN

Stack Overflow用户
提问于 2011-09-19 16:57:48
回答 1查看 1.6K关注 0票数 2

我在用

  • MS Server 2008R2
  • Fluent nHibernate 1.3
  • nHibernate 3.2

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

如何使用Fluent UInt64将UInt64字段映射到十进制(20,0)列?

我试过了

  • CustomSqlType("Decimal").Precision(20).Scale(0)
  • CustomSqlType("decimal(20,0)")
  • CustomType()与CustomSqlType和CustomType.

的排列

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

编辑2,这适用于写端,但当将值读出时,它会以无效的强制转换中断。

代码语言:javascript
复制
.CustomSqlType("decimal").Precision(20).Scale(0)
.CustomType<NHibernate.Type.DoubleType>()
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-20 11:40:11

我会用一个用户类型

代码语言:javascript
复制
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>()

编辑:对不起,忘了注意。我有很多用户类型,所以我实现了我自己的baseclasses

代码语言:javascript
复制
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; }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7474547

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档