2012-12-19 37 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 स्ट्रिंगटॉइन्ट टाइप को int से स्ट्रिंग और इसके विपरीत रूपांतरित करने के लिए निहित रूप से कॉल करेगा। मुझे लगता है कि यह पूरा बिंदु है। मैंने सोचा था कि स्ट्रिंगटॉइन्ट टाइप केवल मैपिंग के लिए था? तब मुझे इसका उपयोग कैसे करना चाहिए?

उत्तर

5

आप सही हैं, ReturnedType को उस प्रकार को वापस करना चाहिए जो NullSafeGet वापस आ जाएगा। उदाहरण कोड you linked to गलत है, ReturnedTypetypeof(bool) वापस करना चाहिए।

इसके अलावा, बराबर विधि सही हो रही बहुत महत्वपूर्ण है और मैं अपने कोड में कुछ बदलाव करने की सिफारिश:

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); 
    } 
1

मैं मई समस्या पाया है:

public Type ReturnedType 

ऊपर StringToIntType देता है, और मुझे लगता है कि यह पूर्णांक होना चाहिए।

हालांकि, पर:

http://lostechies.com/rayhouston/2008/03/23/mapping-strings-to-booleans-using-nhibernate-s-iusertype/

विधि प्रकार है कि IUserType लागू करता है देता है।

कृपया की पुष्टि करें।

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^