2010-08-02 56 views
1

自從很多天以來,我一直在做nhibernate。但今天,我堅持一個令人沮喪的問題,即映射異常。NHibernate.MappingException:無持久化程序在程序集中找不到映射文件

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="IPC.Base.Domains" assembly="IPC"> 
    <class name="MenuItem" table="dbo.COR_MenuItem" default-access="property" default-cascade="save-update" default-lazy="true"> 
    <cache usage="read-only" region="completelyStatic"/> 
    <id name="Id" type="System.Int32"> 
     <generator class="identity" /> 
    </id> 
    <property name="Name" type="System.String" /> 
    <property name="Order" column="DisplayOrder" /> 
    <property name="Key" column="KeyChain" /> 
    <property name="Route" /> 
    <property name="ActionMethod" /> 
    <property name="IsHotlink" /> 
    <many-to-one name="ParentMenuItem" column="ParentMenuItemId" class="MenuItem" cascade="none"/> 
    <bag name="MenuItems" table="dbo.COR_MenuItem" cascade="none"> 
     <cache usage="read-only" region="completelyStatic"/> 
     <key column="ParentMenuItemId" /> 
     <one-to-many class="MenuItem" /> 
    </bag> 
    </class> 
</hibernate-mapping> 

而且我有一個映射類如下:

using System; 
using System.Collections.Generic; 

namespace IPC.Base.Domain 
{ 
    public partial class MenuItem : PersistentObject 
    { 
     public MenuItem() 
     { 
      MenuItems = new List<MenuItem>(); 
     } 
     public virtual string Name { get; set; } 
     public virtual int? Order { get; set; } 
     public virtual string Key { get; set; } 
     public virtual string Route { get; set; } 
     public virtual string ActionMethod { get; set; } 
     public virtual bool IsHotlink { get; set; } 
     public virtual IList<MenuItem> MenuItems { get; set; } 
     public virtual MenuItem ParentMenuItem { get; set; } 

     /// <summary> 
     /// only to be used from the menu builder app 
     /// </summary> 
     /// <param name="id"></param> 
     public virtual void SetId(int id) 
     { 
      Id = id; 
     } 
    } 
} 

我有以下NHibernate的配置與運行的應用程序。但根據我的經驗,nHibernate Config完全沒有問題。

<?xml version="1.0"?> 
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> 
<session-factory name="IPC"> 
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property> 
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 
    <property name="connection.connection_string">**************SQL CONNECTION****************/property> 
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> 
    <property name="cache.provider_class">NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache</property> 
    <property name="cache.use_query_cache">true</property> 
    <property name="show_sql">true</property> 
    <mapping assembly="IPC"/> 
</session-factory> 
</hibernate-configuration> 

我正在使用以下行代碼調用nHibernate會話對象到系統。

static ISessionFactory CurrentFactory 
     { 
      get 
      { 
       if (factory == null) 
       { 
        Configuration cfg = new Configuration(); 
        if (cfgFile == null) 
         cfg = cfg.Configure(); 
        else 
         cfg = cfg.Configure(cfgFile); 

        factory = cfg.BuildSessionFactory(); 
       } 
       return factory; 
      } 
     } 
     public static ISession Create() 
     { 
      var session = CurrentFactory.OpenSession(); 
      return session; 
     } 

Now!當我撥打下面的代碼行時,我得到:

public virtual T Get(int id) 
     { 
      return Session.Get<T>(id); 
     } 


No persister for: IPC.Base.Domain.MenuItem 
Exception Details: NHibernate.MappingException: No persister for: IPC.Base.Domain.MenuItem 

我已經啓用了nHibernate日誌。以下是日誌的基本細節:

00:54:40.011 [4] INFO NHibernate.Cfg.Environment - NHibernate 2.1.0.4000 (2.1.0.4000) 
00:54:40.055 [4] INFO NHibernate.Cfg.Environment - Bytecode provider name : lcg 
00:54:40.057 [4] INFO NHibernate.Cfg.Environment - Using reflection optimizer 
00:54:40.682 [4] DEBUG NHibernate.Cfg.Configuration - dialect=NHibernate.Dialect.MsSql2005Dialect 
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - connection.driver_class=NHibernate.Driver.SqlClientDriver 
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - connection.connection_string=*********************SQL Connection***************** 
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - proxyfactory.factory_class=NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu 
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - cache.provider_class=NHibernate.Caches.SysCache.SysCacheProvider, NHibernate.Caches.SysCache 
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - cache.use_query_cache=true 
00:54:40.683 [4] DEBUG NHibernate.Cfg.Configuration - show_sql=true 
00:54:40.684 [4] DEBUG NHibernate.Cfg.Configuration - IPC<-IPC 
00:54:40.685 [4] INFO NHibernate.Cfg.Configuration - Searching for mapped documents in assembly: IPC 
00:54:40.689 [4] WARN NHibernate.Cfg.Configuration - No mapped documents found in assembly: IPC, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null 
00:54:40.691 [4] INFO NHibernate.Cfg.Configuration - Configured SessionFactory: IPC 
00:54:40.691 [4] DEBUG NHibernate.Cfg.Configuration - properties: System.Collections.Generic.Dictionary`2[System.String,System.String] 
00:54:45.716 [4] INFO NHibernate.Cfg.Configuration - checking mappings queue 
00:54:45.716 [4] INFO NHibernate.Cfg.Configuration - processing one-to-many association mappings 
00:54:45.717 [4] INFO NHibernate.Cfg.Configuration - processing one-to-one association property references 
00:54:45.717 [4] INFO NHibernate.Cfg.Configuration - processing foreign key constraints 
00:54:45.747 [4] INFO NHibernate.Dialect.Dialect - Using dialect: NHibernate.Dialect.MsSql2005Dialect 

我已經閱讀了幾乎所有帖子的堆棧! :)甚至做谷歌相同。最後的話我到達那裏是我的大會命名問題!我確信我所遵循的步驟存在問題。但仍然在尋找觸發器!

謝謝! 提前!

回答

11

可能您忘記將您的hbm映射設置爲嵌入式資源到DLL。

+0

很高興描述它的固定方式 - 類似於OP如何回答他自己的問題,但答案仍然缺少實際步驟 - 即Build Action - > Embedded Resource – shanabus 2012-10-17 15:42:01

2

最後我解決了這個問題!它基本上是一種愚蠢的東西,通常我傾向於這樣做,然後忘記在舞臺上重新獲得它。

是的,我與裝配問題有關。沒有什麼,但當我試圖構建數據層應用程序時它並沒有包含程序集內的xml文件。

一種解決問題去你 *的.hbm.xml文件和做文件 嵌入式資源的點擊右鍵並改變構建選項修復。

Alrite!

非常感謝!

+1

這就是Sly說的。 – 2011-01-08 23:48:15

相關問題