什么是用NHIBERNATE映射UINT32类型为SQL-Server int类型的最佳方法。

值是图像宽度/高度,因此负值在这里没有意义。

但是也许我应该使用int,因为核酸不支持未分配的int。

有帮助吗?

解决方案

您可以使用IuserType映射列。

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

和映射的iusertype UInt32?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 3.0,Visual Studio 2005和.NET 2.0.x。

我发现我可以使用.NET的UINT32类,并且不包括HBM.XML中的类型属性。

// .NET 2.0 Property syntax
public class MyClass
{
   // NHibernate needs public virtual properties. 
   private UInt32 _Id;
   public virtual UInt32 Id { get { return (_Id); } set { _Id = value; } }
}


// hbml.xml
<class name ="MyClass">
   <id name="Id" />
</class>


// SQL to create the table
CREATE TABLE `PumpConnection` (
`Id` **INT**(10) **UNSIGNED** NOT NULL AUTO_INCREMENT,
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top