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

发布于 2024-12-05 12:02:32 字数 817 浏览 0 评论 0 原文

我正在使用

  • MS SQL Server 2008R2
  • Fluent 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)")
  • CustomType() 以及 CustomSqlType 和 CustomType 的排列。

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

编辑2 这适用于写入端,但在读回值时会因无效转换而中断

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

糖粟与秋泊 2024-12-12 12:02:32

我会选择用户类型

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

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文