2012-12-19 45 views
6

我有這個類實現IUserType:NHibernate的:IUserType不工作

public class StringToIntType : IUserType 
    { 
     /// <summary> 
     /// mutable object = an object whose state CAN be modified after it is created 
     /// </summary> 
     public bool IsMutable 
     { 
      get { return false; } 
     } 

     public Type ReturnedType 
     { 
      get { return typeof(StringToIntType); } 
     } 

     public SqlType[] SqlTypes 
     { 
      get { return new[] { NHibernateUtil.String.SqlType }; } 
     } 

     public object NullSafeGet(IDataReader rs, string[] names, object owner) 
     { 
      var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]); 

      if (obj == null) return null; 

      var s = (string)obj; 

      int i; 
      if (Int32.TryParse(s, out i)) 
       return i; 
      return -1; 
     } 

     public void NullSafeSet(IDbCommand cmd, object value, int index) 
     { 
      if (value == null) 
      { 
       ((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value; 
      } 
      else 
      { 
       var i = (int)value; 
       ((IDataParameter)cmd.Parameters[index]).Value = i.ToString(); 
      } 
     } 

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

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

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

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

     public new bool Equals(object x, object y) 
     { 
      if (ReferenceEquals(x, y)) return true; 

      if (x == null || y == null) return false; 

      return x.Equals(y); 
     } 

     public int GetHashCode(object x) 
     { 
      return x == null ? typeof(int).GetHashCode() + 473 : x.GetHashCode(); 
     } 
    } 

我的映射:

public BarausLangMap() 
     { 
      Table("BARAUSLANG"); 

      Id(x => x.ula).CustomType<StringToIntType>(); 

      Map(x => x.bezeichnung); 
      Map(x => x.sprache); 
      Map(x => x.la); 
      Where("la = 'SPE'"); 
     } 

我的屬性:

public virtual int ula { get; set; } 
    public virtual String bezeichnung { get; set; } 
    public virtual Int32? sprache { get; set; } 
    public virtual String la { get; set; } 

問題:當我做

var b = session.Get<BarausLang>(5); 

它說

{NHibernate.TypeMismatchException: Provided id of the wrong type. 
Expected: MobileServiceServer.Models.StringToIntType, got System.Int32 

問題是什麼?我認爲nHibernate會隱式地調用StringToIntType來從int轉換爲字符串,反之亦然。我認爲這是重點。我以爲StringToIntType僅用於映射?那麼我應該如何使用它?

回答

5

你是對的,ReturnedType應該返回NullSafeGet將返回的類型。示例代碼you linked to不正確,ReturnedType應返回typeof(bool)

而且,越來越Equals方法正確是非常重要的,我建議一個小的改變了代碼:

public new bool Equals(object x, object y) 
    { 
     if (ReferenceEquals(x, y)) return true; 

     var xString = x as string; 
     var yString = y as string; 
     if (xString == null || yString == null) return false; 

     return xString.Equals(yString); 
    } 
相關問題