NHibernate MappingException。没有毅力

时间:2009-09-16 23:49:32

标签: nhibernate nhibernate-mapping

我正在努力让NHibernate工作。我有这堂课:

mm.k.Domain.Kampagne

(名称空间/程序集是mm.k.Domain)

在另一个Visual Studio项目(Assembly 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");

一切都很好!

有什么想法吗?提前谢谢。

4 个答案:

答案 0 :(得分:7)

如果有人像我一样遇到Hibernate.NET这个问题。 确保在属性窗口中选择了文件构建操作为“嵌入式资源”

答案 1 :(得分:3)

不确定你的nhibernate.cfg.xml文件是什么样的,但我通常有这样的项目

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

根据您提供的信息。 NHibernate使用它来加载来自这个特定程序集的映射文件。

这应该为您提供所需的映射。

答案 2 :(得分:2)

我遇到了问题。但突然发现映射文件没有嵌入。 转到.hbm.xml文件。单击属性。然后进阶 - &gt;选择“嵌入资源”

答案 3 :(得分: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时你必须使用: 例如,您在产品类

中使用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();
相关问题