2010-07-27 53 views
3

我在應用NHibernate Fluent Automapping時遇到了一些問題。它在測試項目中效果很好。但現在..需要關於'沒有persister'的幫助:'Fluent Nhibernate Automapping的例外

Test method [PROJECTNAME].SubscriptionTest.SubscriptionConstructorTest threw exception: NHibernate.MappingException: No persister for: [PROJECTLIB].SubscriptionManagerRP 

類(然後再次,相同的異常以更簡單的TestClass出現 - 這樣的問題不應該在這裏):

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.4927")] 
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://docs.oasis-open.org/wsn/b-2")] 
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://docs.oasis-open.org/wsn/b-2", IsNullable = false)] 
    [System.Runtime.Serialization.DataContractAttribute(Name = "SubscriptionManagerRP", Namespace = "http://docs.oasis-open.org/wsn/b-2")] 
    public class SubscriptionManagerRP 
    { 

     private string id; 

     public string Id 
     { 
      get 
      { 
       return id; 
      } 
      set 
      { 
       id = value; 
      } 
     } 

     public Boolean Save() 
     { 
      DatabaseAccess access = new DatabaseAccess(); 
      var sessionFactory = access.getSessionFactory(); 

      //try 
      //{ 
       using (var session = sessionFactory.OpenSession()) 
       { 
        using (var transaction = session.BeginTransaction()) 
        { 


         SaveTextMess(this.ToString()); 
         session.Save(this); 
         transaction.Commit(); 
         return true; 
        } 
       } 
      //} 
      //catch (Exception e) 
      //{ 
      // SaveTextMess("ERROR: " + e); 
      // Console.WriteLine(e); 
      //} 

      //SaveTextMess("false"); 
      return false; 
     } 

     private void SaveTextMess(String output) 
     { 
      //Just for Demo purposes, saving text file per message that should be sent 

      // create a writer and open the file 
      TextWriter tw = new StreamWriter("C:\\Temp\\CespSubscriptionManagerRPMessage.txt"); 

      // write a line of text to the file 
      tw.WriteLine(output); 

      // close the stream 
      tw.Close(); 
     } 

//################################### 




     [EditorBrowsable(EditorBrowsableState.Never)] 
     private EndpointReferenceType consumerReferenceField; 

     [EditorBrowsable(EditorBrowsableState.Never)] 
     private FilterType filterField; 

     [EditorBrowsable(EditorBrowsableState.Never)] 
     private SubscriptionPolicyType subscriptionPolicyField; 

     [EditorBrowsable(EditorBrowsableState.Never)] 
     private System.DateTime creationTimeField; 

     [EditorBrowsable(EditorBrowsableState.Never)] 
     private bool creationTimeFieldSpecified; 

     private static System.Xml.Serialization.XmlSerializer serializer; 

     /// <summary> 
     /// .ctor class constructor 
     /// </summary> 
     public SubscriptionManagerRP() 
     { 
      this.subscriptionPolicyField = new SubscriptionPolicyType(); 
      this.filterField = new FilterType(); 
      this.consumerReferenceField = new EndpointReferenceType(); 
     } 

     [System.Runtime.Serialization.DataMemberAttribute()] 
     public EndpointReferenceType ConsumerReference 
     { 
      get 
      { 
       return this.consumerReferenceField; 
      } 
      set 
      { 
       this.consumerReferenceField = value; 
      } 
     } 

     [System.Runtime.Serialization.DataMemberAttribute()] 
     public FilterType Filter 
     { 
      get 
      { 
       return this.filterField; 
      } 
      set 
      { 
       this.filterField = value; 
      } 
     } 

     [System.Runtime.Serialization.DataMemberAttribute()] 
     public SubscriptionPolicyType SubscriptionPolicy 
     { 
      get 
      { 
       return this.subscriptionPolicyField; 
      } 
      set 
      { 
       this.subscriptionPolicyField = value; 
      } 
     } 

     [System.Runtime.Serialization.DataMemberAttribute()] 
     public System.DateTime CreationTime 
     { 
      get 
      { 
       return this.creationTimeField; 
      } 
      set 
      { 
       this.creationTimeField = value; 
      } 
     } 

     [System.Xml.Serialization.XmlIgnoreAttribute()] 
     [System.Runtime.Serialization.DataMemberAttribute()] 
     public bool CreationTimeSpecified 
     { 
      get 
      { 
       return this.creationTimeFieldSpecified; 
      } 
      set 
      { 
       this.creationTimeFieldSpecified = value; 
      } 
     } 

     private static System.Xml.Serialization.XmlSerializer Serializer 
     { 
      get 
      { 
       if ((serializer == null)) 
       { 
        serializer = new System.Xml.Serialization.XmlSerializer(typeof(SubscriptionManagerRP)); 
       } 
       return serializer; 
      } 
     } 

     #region Serialize/Deserialize 
     /// <summary> 
     /// Serializes current SubscriptionManagerRP object into an XML document 
     /// </summary> 
     // <returns>string XML value</returns> 
     public virtual string WriteObject() 
     { 
      System.IO.StreamReader streamReader = null; 
      System.IO.MemoryStream memoryStream = null; 
      try 
      { 
       memoryStream = new System.IO.MemoryStream(); 
       Serializer.Serialize(memoryStream, this); 
       memoryStream.Seek(0, System.IO.SeekOrigin.Begin); 
       streamReader = new System.IO.StreamReader(memoryStream); 
       return streamReader.ReadToEnd(); 
      } 
      finally 
      { 
       if ((streamReader != null)) 
       { 
        streamReader.Dispose(); 
       } 
       if ((memoryStream != null)) 
       { 
        memoryStream.Dispose(); 
       } 
      } 
     } 

     /// <summary> 
     /// Deserializes workflow markup into an SubscriptionManagerRP object 
     /// </summary> 
     // <param name="xml">string workflow markup to deserialize</param> 
     // <param name="obj">Output SubscriptionManagerRP object</param> 
     // <param name="exception">output Exception value if deserialize failed</param> 
     // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns> 
     public static bool ReadObject(string xml, out SubscriptionManagerRP obj, out System.Exception exception) 
     { 
      exception = null; 
      obj = default(SubscriptionManagerRP); 
      try 
      { 
       obj = ReadObject(xml); 
       return true; 
      } 
      catch (System.Exception ex) 
      { 
       exception = ex; 
       return false; 
      } 
     } 

     public static bool ReadObject(string xml, out SubscriptionManagerRP obj) 
     { 
      System.Exception exception = null; 
      return ReadObject(xml, out obj, out exception); 
     } 

     public static SubscriptionManagerRP ReadObject(string xml) 
     { 
      System.IO.StringReader stringReader = null; 
      try 
      { 
       stringReader = new System.IO.StringReader(xml); 
       return ((SubscriptionManagerRP)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader)))); 
      } 
      finally 
      { 
       if ((stringReader != null)) 
       { 
        stringReader.Dispose(); 
       } 
      } 
     } 

     /// <summary> 
     /// Serializes current SubscriptionManagerRP object into file 
     /// </summary> 
     // <param name="fileName">full path of outupt xml file</param> 
     // <param name="exception">output Exception value if failed</param> 
     // <returns>true if can serialize and save into file; otherwise, false</returns> 
     public virtual bool SaveToFile(string fileName, out System.Exception exception) 
     { 
      exception = null; 
      try 
      { 
       SaveToFile(fileName); 
       return true; 
      } 
      catch (System.Exception e) 
      { 
       exception = e; 
       return false; 
      } 
     } 

     public virtual void SaveToFile(string fileName) 
     { 
      System.IO.StreamWriter streamWriter = null; 
      try 
      { 
       string xmlString = WriteObject(); 
       System.IO.FileInfo xmlFile = new System.IO.FileInfo(fileName); 
       streamWriter = xmlFile.CreateText(); 
       streamWriter.WriteLine(xmlString); 
       streamWriter.Close(); 
      } 
      finally 
      { 
       if ((streamWriter != null)) 
       { 
        streamWriter.Dispose(); 
       } 
      } 
     } 

     /// <summary> 
     /// Deserializes workflow markup from file into an SubscriptionManagerRP object 
     /// </summary> 
     // <param name="xml">string workflow markup to deserialize</param> 
     // <param name="obj">Output SubscriptionManagerRP object</param> 
     // <param name="exception">output Exception value if deserialize failed</param> 
     // <returns>true if this XmlSerializer can deserialize the object; otherwise, false</returns> 
     public static bool LoadFromFile(string fileName, out SubscriptionManagerRP obj, out System.Exception exception) 
     { 
      exception = null; 
      obj = default(SubscriptionManagerRP); 
      try 
      { 
       obj = LoadFromFile(fileName); 
       return true; 
      } 
      catch (System.Exception ex) 
      { 
       exception = ex; 
       return false; 
      } 
     } 

     public static bool LoadFromFile(string fileName, out SubscriptionManagerRP obj) 
     { 
      System.Exception exception = null; 
      return LoadFromFile(fileName, out obj, out exception); 
     } 

     public static SubscriptionManagerRP LoadFromFile(string fileName) 
     { 
      System.IO.FileStream file = null; 
      System.IO.StreamReader sr = null; 
      try 
      { 
       file = new System.IO.FileStream(fileName, FileMode.Open, FileAccess.Read); 
       sr = new System.IO.StreamReader(file); 
       string xmlString = sr.ReadToEnd(); 
       sr.Close(); 
       file.Close(); 
       return ReadObject(xmlString); 
      } 
      finally 
      { 
       if ((file != null)) 
       { 
        file.Dispose(); 
       } 
       if ((sr != null)) 
       { 
        sr.Dispose(); 
       } 
      } 
     } 
     #endregion 

     #region Clone method 
     /// <summary> 
     /// Create a clone of this SubscriptionManagerRP object 
     /// </summary> 
     public virtual SubscriptionManagerRP Clone() 
     { 
      return ((SubscriptionManagerRP)(this.MemberwiseClone())); 
     } 
     #endregion 
    } 

從類的保存方法上述(看起來簡單TestClass的是,在測試項目中的工作原理相同):

public Boolean Save() 
     { 
      DatabaseAccess access = new DatabaseAccess(); 
      var sessionFactory = access.getSessionFactory(); 

      //try 
      //{ 
       using (var session = sessionFactory.OpenSession()) 
       { 
        using (var transaction = session.BeginTransaction()) 
        { 


         SaveTextMess(this.ToString()); 
         session.Save(this); 
         transaction.Commit(); 
         return true; 
        } 
       } 
      //} 
      //catch (Exception e) 
      //{ 
      // SaveTextMess("ERROR: " + e); 
      // Console.WriteLine(e); 
      //} 

      //SaveTextMess("false"); 
      return false; 
     } 

我在哪裏設置連接字符串和會話工廠:

class SessionFactoryController 
    { 

     public SessionFactoryController() 
     { 

     } 

     public ISessionFactory GiveFactory() 
     { 
      return CreateSessionFactory(); 
     } 

     private static void ReferByteCode(){ 

      //Just to make sure the ByteCodeCastle is loaded 
      ProxyFactory fake = new ProxyFactory(); 
     } 

     private static ISessionFactory CreateSessionFactory() 
     { 
      ReferByteCode(); 

      var cfg = new FluentNhibernateConfiguration(); 

      return Fluently.Configure() 
       .Database(
       FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2005 
        .ConnectionString("[SERVER];Database=Pets;User ID=NHibernateTester;Password=[PASSWORD];Trusted_Connection=False;") 
      ) 

       .Mappings(m => 
        m.AutoMappings 
        .Add(AutoMap.AssemblyOf<SubscriptionManagerRP>(cfg)) 
       ) 

       .BuildSessionFactory(); 
     } 

    } 

配置:

class NotifyFluentNhibernateConfiguration : DefaultAutomappingConfiguration 
    { 
     public override bool ShouldMap(Type type) 
     { 
      return type.Namespace == "System.Xml.XmlAttribute"; 
     } 
    } 

的配置是處理較早的映射例外 'System.Xml.XmlAttribute'。

如果數據庫設置不正確,是否可能出現此錯誤?我已經完成了一個測試,使用一個類,我知道NHibernate Automapping(從另一個項目)測試數據庫(與其他項目相同)。我仍然得到同樣的例外,但是對於'簡單'類。

所以這不是課堂。

它不是連接字符串,因爲它是從另一個很棒的項目中複製而來的。

它不是數據庫上的設置(或至少安全/訪問設置)。我認爲如果我錯誤地配置了表格,它應該會出現另一個錯誤,如前所述,當我將它指向另一個項目中的某些東西時,我會得到相同的異常。

我已經刪除了一切,並重寫了一次,只是爲了確保我沒有做一些小小的愚蠢錯誤。如果是這樣,我做了兩次。

如上所述,如你所見。 這段代碼非常非常簡單。唯一複雜的是班級。即使我將它改爲一個非常非常簡單的類,我也會得到相同的異常。

有什麼想法嗎?

+0

你有沒有想過這個?我遇到了同樣的問題。 – 2010-11-02 14:34:02

+0

你有一些課程來標記公衆(或內部也許)嗎? http://2bhere4u.blogspot.com/2010/01/nhibernatemappingexception-no-persister.html – 2011-05-02 20:29:57

+0

我認爲它可能是'返回type.Namespace ==「System.Xml.XmlAttribute」'。其缺失的|| type.Namespace ==「PROJECTLIB」' – Firo 2011-07-25 13:17:35

回答

0

如果您的類不在System.Xml.XmlAttribute命名空間中,它將不會被automapping映射。很難相信你的課程在這個名字空間中。如果你希望它被映射,ShouldMap應該爲你的類型返回true。