Frage

Was ist der beste Weg, um UINT32-Typ auf SQL-Server-Int-Typ mit NHiNRNATE zuzuordnen.

Der Wert ist eine Bildbreite/Höhe, so dass negativer Wert hier nicht sinnvoll ist.

Aber vielleicht sollte ich int int verwenden, weil Nhibenate nicht nicht zugewiesene INTs unterstützt.

War es hilfreich?

Lösung

Sie können die Spalte mit einem iusertype zuordnen.

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

Und der iusertype, der kartiert UInt32? und 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 );
    }
}

Andere Tipps

Ich bin ein Jahr zu spät, aber da ich die gleiche Frage hatte und eine andere Antwort gefunden habe, dachte ich, ich würde sie hinzufügen. Es scheint einfacher. Vielleicht hat es einen Fehler, den ich noch nicht entdeckt habe.

Ich verwende NhiberNate 3.0, Visual Studio 2005 und .NET 2.0.x.

Ich stellte fest, dass ich die UINT32 -Klasse von .NET verwenden konnte und das Typattribut nicht in die HBm.xml einbezieht.

// .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,
)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top