2015-06-04 98 views
1

我不知道錯誤在哪裏。NHibernate無法編譯映射

我使用NHibernate C#並將程序集追加到配置中。 問題NHibernateException:無法編譯映射Note.hbm.xml

public static class NHibernateHelper 
{ 
    private static Configuration _cfg; 
    private static ISessionFactory _sessionFactory; 

    private static ISessionFactory SessionFactory { 
     get { 
      if (_sessionFactory == null) { 
       _cfg = new Configuration(); 
       _cfg.Configure(); 

       _cfg.AddAssembly(typeof(User).Assembly); 
       _cfg.AddAssembly(typeof(Changelog).Assembly); 
       _cfg.AddAssembly(typeof(Note).Assembly); 


       _sessionFactory = _cfg.BuildSessionFactory(); 
      } 

      return _sessionFactory; 
     } 
    } 


    public static ISession OpenSession() { 
     return SessionFactory.OpenSession(); 
    } 

    public static void DatabaseSchemaLoader() { 
     ISession session = OpenSession(); 
     new SchemaExport(_cfg).Create(true, true); 
     session.Close(); 
    } 
} 

我的筆記類是shared.data.content

namespace CRMSystem.shared.data.content 
{ 
    public class Note 
    {  
     public virtual int id { get; set; } 
     public virtual string title{ get; set;} 
     public virtual string text { get; set; } 
     public virtual int priority { get; set; } 
     public virtual long createdTime { get; set; } 
     public virtual long lastEditTime { get; set; }  
    } 
} 

Note.hbm.xml(集生成操作:嵌入的資源,複製到輸出:複製總是)

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="CRMSystem" 
        namespace="CRMSystem.shared.data.content"> 

    <class name="Note" table="`Note`" lazy="false"> 


    <id name="id" column="`id`" type="int"> 
     <generator class="identity"></generator> 
    </id> 

    <property name="title" column="`title`" type="String"/> 
    <property name="text" column="`text`" type="String" /> 
    <property name="priority" column="`priority`" type="int" /> 
    <property name="createdTime" column="`createdTime`" type="long" /> 
    <property name="lastEditTime" column="`lastEditTime`" type="long" /> 
    </class> 
</hibernate-mapping> 

感謝您的回答。

我今天在很多方面嘗試,發現它的工作原理:

_cfg.Configure(); 
_cfg.AddAssembly(typeof(User).Assembly); 
//_cfg.AddAssembly(typeof(Changelog).Assembly); 
//_cfg.AddAssembly(typeof(Note).Assembly); 

當我補充說:(任何級別)

_cfg.AddAssembly(typeof(Changelog).Assembly); 

每次我得到一個錯誤:無法編譯註意事項.hbm.xml 我可以保存到數據庫的每個類(用戶,更新日誌,注意)。 我無法解釋。

有沒有人有這種情況?

完全例外:

NHibernate.MappingException: Could not compile the mapping document: CRMSystem.shared.data.mapping.Note.hbm.xml ---> NHibernate.DuplicateMappingException: Duplicate class/entity mapping CRMSystem.shared.data.content.Note 
    w NHibernate.Cfg.Mappings.AddClass(PersistentClass persistentClass) 
    w NHibernate.Cfg.XmlHbmBinding.RootClassBinder.Bind(HbmClass classSchema, IDictionary`2 inheritedMetas) 
    w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddRootClasses(HbmClass rootClass, IDictionary`2 inheritedMetas) 
    w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.AddEntitiesMappings(HbmMapping mappingSchema, IDictionary`2 inheritedMetas) 
    w NHibernate.Cfg.XmlHbmBinding.MappingRootBinder.Bind(HbmMapping mappingSchema) 
    w NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) 
    --- Koniec śladu stosu wyjątków wewnętrznych --- 
    w NHibernate.Cfg.Configuration.LogAndThrow(Exception exception) 
    w NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName) 
    w NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc) 
    w NHibernate.Cfg.Configuration.ProcessMappingsQueue() 
    w NHibernate.Cfg.Configuration.AddDocumentThroughQueue(NamedXmlDocument document) 
    w NHibernate.Cfg.Configuration.AddXmlReader(XmlReader hbmReader, String name) 
    w NHibernate.Cfg.Configuration.AddInputStream(Stream xmlInputStream, String name) 
    w NHibernate.Cfg.Configuration.AddResource(String path, Assembly assembly) 
    w NHibernate.Cfg.Configuration.AddAssembly(Assembly assembly) 

當我沒有什麼補充,我得到異常:沒有留存

+0

可能會刪除列屬性Note.hbm.xml中的單個線索 – DanielVorph

+0

首先 - 將您的答案移動到您的問題中。刪除該答案。其次,你還可以附加完整的內部異常堆棧嗎?像我一樣?將會有明確的答案 –

+0

您能否顯示**完整**異常?確切地說,在您引用後*無法編譯Note.hbm.xml * - 確切地說,下一行清楚地說明了問題所在。觀察我自己的答案,你會看到我爲你創造的這種提取物...... –

回答

1

EXTEND

基於最新的更新,我們可以看到,NHiberante在埋怨:

... Duplicate class/entity mapping CRMSystem.shared.data.content.Note ...

而這意味着,在我們的情況下,其他一些文件可以c保持NOTE映射,例如在user.hbm.xml可能會有一些被遺忘的映射

<class name="Note" ... 

原件

的代碼是確定爲是。我在本地嘗試了Note類和Note.hbm.xml。按預期工作。

這個問題似乎是一些拼寫錯誤,我很難再現(例如不同的程序集名稱)。或者,儘管Note.hbm.xml的責任...它也可能是一些其他映射

// there could be also some issue 
_cfg.AddAssembly(typeof(User).Assembly); 
_cfg.AddAssembly(typeof(Changelog).Assembly); 

無論如何,嘗試觀察INNER異常。將會有明確的問題描述。例如。這種映射

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
       // instead of this 
       // assembly="CRMSystem" 
       // we have this 
       assembly="WrongAssembly" 
       namespace="CRMSystem.shared.data.content"> 

我們會得到充分的例外:

NHibernate.MappingException: Could not compile the mapping document: ...Note.hbm.xml ---> NHibernate.MappingException: persistent class
CRMSystem.shared.data.content.Note, WrongAssembly not found ---> System.IO.FileNotFoundException: Could not load file or assembly 'WrongAssembly' or one of its dependencies. The system cannot find the file specified.WRN: Assembly binding logging is turned OFF.

注:嵌入資源屬性是必須,不要複製建議。這個文件將是dll的一部分,它不必被複製爲一個文件。

+0

任何進度?你有沒有找到鬼'