2009-09-16 47 views
1

我想讓NHibernate工作。我有這個類:NHibernate MappingException。沒有Persister

mm.k.Domain.Kampagne 

空間(namespace /組件mm.k.Domain)

在另一個Visual Studio項目(大會mm.k.Infrastructure)我得到了我的映射文件(在Mappings目錄),我的hibernate.cfg.xml和一些存儲庫。

我的繼承人映射文件:

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
        assembly="mm.k.Domain" 
        namespace="mm.k.Domain"> 

    <class name="Kampagne" table="Kampagner"> 
    <id name="Id"> 
     <generator class="identity" /> 
    </id> 
    <property name="Navn" not-null="true" /> 
    <property name="Logo" /> 
    </class> 

</hibernate-mapping> 

當我配置我的會議,我這樣做:

_configuration.AddAssembly(typeof(mm.k.Domain.Kampagne).Assembly); 

而且那是什麼不行! 當調用:

var test = session.Get<Kampagne>(kampagneId); 

我得到以下錯誤: 「沒有留存爲:mm.k.Domain.Kampagne」 不管你喜歡不註冊嵌入式映射FILD。請注意,我已將映射文件設置爲Embedded Resource的構建操作。

如果我改變上述行來:

_configuration.AddFile(@"fullpath\mm.k.Infrastructure\Mappings\Kampagne.hbm.xml"); 

一切工作完全正常!

任何想法?提前致謝。

回答

3

不知道你nhibernate.cfg.xml文件看起來像什麼,但我通常有你給根據您的信息,這樣

<mapping assembly="mm.K.Infrastructure"/> 

的項目。 NHibernate使用它從這個特定的程序集加載映射文件。

這應該給你你需要的映射。

+0

這樣做的伎倆。謝謝!奇怪NHibernate入門教程沒有提到這個? – 2009-09-21 18:59:32

7

如果有人會像我那樣用Hibernate.NET來解決這個問題。 確保您在屬性窗口中爲您的文件構建操作選擇了「嵌入式資源」

+0

完美的,這對我來說,不像其他答案在這裏。尼斯。 – thomas 2013-10-24 09:25:13

1

每當你使用hbm.xml文件中,你將設置你的配置類是這樣的:

Configuration cfg = new Configuration(); 
cfg.Configure(); 
// Add class mappings to configuration object 
cfg.AddAssembly(Assembly.GetCallingAssembly()); 
ISessionFactory sessionFactory = cfg.BuildSessionFactory(); 

每當你使用像CLASSE Nhibernate.Mapping.Attributes你將不得不使用: 例如,你有使用映射。屬性產品分類

Configuration cfg = new Configuration(); 
cfg.Configure(); 
// Add class mappings attributes to configuration object 
cfg.AddInputStream(HbmSerializer.Default.Serialize(typeof(Model.Product); 
ISessionFactory sessionFactory = cfg.BuildSessionFactory(); 
2

我得到了問題。但突然發現映射文件沒有嵌入。 轉至.hbm.xml文件。點擊屬性。然後高級 - >選擇「嵌入式資源」

相關問題