NHibernate - 如何在数据库中存储 UInt32 [英] NHibernate - How to store UInt32 in database

查看:15
本文介绍了NHibernate - 如何在数据库中存储 UInt32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 NHibernate 将 UInt32 类型映射到 sql-server int 类型的最佳方法是什么.

What is the best way to map UInt32 type to sql-server int type with NHibernate.

该值是图片的宽度/高度,因此这里的负值没有意义.

The value is a picture width/height so negative value are not make sense here.

但也许我应该使用 int 因为 NHibenate 不支持未分配的 int.

But maybe I should use int because NHibenate doesn't support unassigned ints.

推荐答案

您可以使用 IUserType 映射列.

You can map the column with an IUserType.

<class name="UnsignedCounter">
    <property name="Count" type="mynamespace.UInt32Type, mydll"  />
</class>

以及映射UInt32?UInt32的IUserType.

And the IUserType which maps UInt32? and UInt32.

class UInt32Type : IUserType
{
    public object NullSafeGet( System.Data.IDataReader rs, string[] names, object owner )
    {
        int? i = (int?) NHibernateUtil.Int32.NullSafeGet( rs, names[0] );
        return (UInt32?) i;
    }

    public void NullSafeSet( System.Data.IDbCommand cmd, object value, int index )
    {
        UInt32? u = (UInt32?) value;
        int? i = (Int32?) u;
        NHibernateUtil.Int32.NullSafeSet( cmd, i, index );
    }

    public Type ReturnedType
    {
        get { return typeof(Nullable<UInt32>); }
    }

    public SqlType[] SqlTypes
    {
        get { return new SqlType[] { SqlTypeFactory.Int32 }; }
    }

    public object Assemble( object cached, object owner )
    {
        return cached;
    }

    public object DeepCopy( object value )
    {
        return value;
    }

    public object Disassemble( object value )
    {
        return value;
    }

    public int GetHashCode( object x )
    {
        return x.GetHashCode();
    }

    public bool IsMutable
    {
        get { return false; }
    }

    public object Replace( object original, object target, object owner )
    {
        return original;
    }

    public new bool Equals( object x, object y )
    {
        return x != null && x.Equals( y );
    }
}

这篇关于NHibernate - 如何在数据库中存储 UInt32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆