2010-02-22 11 views
0

NHibernate를 사용하여 UInt32 유형을 sql-server int 유형에 매핑하는 가장 좋은 방법은 무엇입니까?NHibernate - 데이터베이스에 UInt32를 저장하는 방법

값은 그림의 너비/높이이므로 음수 값은 여기에 적합하지 않습니다.

하지만 NHibernate가 할당되지 않은 int를 지원하지 않기 때문에 int를 사용해야 할 수도 있습니다.

답변

3

IUserType을 사용하여 열을 매핑 할 수 있습니다.

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

그리고 UInt32?UInt32를 매핑 IUserType

.

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); 
    } 
} 
+0

'NullSafeSet'을 사용하여 uint 열거 형에서 변환 할 때 문제가 있습니다.이 경우 값을 null 가능 유형으로 변환 할 수 없습니다. 'uint? u = null; if (value! = null) u = (단위) 값; 대신 – fmuecke

+0

Equals가 제대로 작동하지 않습니다. 작업 버전은 다음과 같을 수 있습니다. public new bool Equals (object x, object y) { if (object.ReferenceEquals (x, y)) { return true; (x == null || y == null) { } } return (uint) x == (uint) y; }' – fmuecke

+0

@fmuecke :이 경우 'Equals'는 항상 박스 값을받습니다. 따라서'object.ReferenceEquals'는 항상 false를 반환하며 결코 null이 아닙니다. – Groo

2

저는 1 년 늦었습니다.하지만 같은 질문을하고 다른 대답을 발견 했으므로 추가하겠습니다. 그것은 더 단순 해 보인다. 어쩌면 아직 발견하지 못한 결함이있을 수 있습니다.

NHibernate 3.0, Visual Studio 2005 및 .NET 2.0.x를 사용하고 있습니다.

.NET의 UInt32 클래스를 사용할 수 있고 hbm.xml에 type 속성을 포함 할 수 없다는 것을 알았습니다.

// .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, 
) 
+0

스키마 생성을 사용하려면'sql-type'을 시도 했습니까? –

관련 문제