简体   繁体   English

NHibernate搜索错误的文件名

[英]NHibernate searches for wrong filename

I'm new to nhibernate and tried a few tutorials but none of them helped with that problem: 我是nhibernate的新手,并尝试了一些教程,但没有一个帮助解决该问题:

I did my mappings, classes, ... and when I'm trying to add my class to configuration, hibernate is searching for the wrong filename (I guess in the wrong directory). 我做了我的映射,类……,当我试图将我的类添加到配置中时,hibernate正在搜索错误的文件名(我猜在错误的目录中)。

This is my hibernate.cfg.xml: 这是我的hibernate.cfg.xml:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
      <property name="connection.connection_string">Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication2-20170915093558.mdf;Initial Catalog=aspnet-WebApplication2-20170915093558;Integrated Security=True</property>
      <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
      <mapping assembly="WebApplication1"/>
    </session-factory>
  </hibernate-configuration>
</configuration>

This is my Test -entity: 这是我的Test实体:

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

  <class name="Test" table="Test" lazy="false">
    <id name="Id">
      <generator class="native" />
    </id>
    <property name="Name" />
    <property name="DatasetCreationDate" />
  </class>

</hibernate-mapping>

namespace WebApplication1.Domain
{
    public class Test
    {
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual DateTime DatasetCreationDate { get; set; }
    }
}

This is the code I'm now calling: 这是我现在要调用的代码:

Configuration configuration = new Configuration();
configuration.AddAssembly(Assembly.GetCallingAssembly());
configuration.AddClass(typeof(Test));
configuration.Configure();
SessionFactory = configuration.BuildSessionFactory();

On this line I get following error by nhibernate: 在这行中,我通过nhibernate得到以下错误:

configuration.AddClass(typeof(Test));

Resource not found: WebApplication1.Domain.Test.hbm.xml 找不到资源:WebApplication1.Domain.Test.hbm.xml

This is my structure: 这是我的结构:

在此处输入图片说明

Any idea what is going wrong here? 知道这里出了什么问题吗? I think my file-structure is also wrong. 我认为我的文件结构也是错误的。

So I found the problems myself (with a bit of help by Radim Köhler): 因此,我自己找到了问题(在RadimKöhler的帮助下):

1st problem 第一个问题

The properties for the .hbm.xml -files .hbm.xml -files的属性

  • Build Action: Embedded Resource . 构建操作: Embedded Resource
  • Copy to Output Directory: Do not copy 复制到输出目录: Do not copy

2nd problem 第二个问题

I was adding the assembly twice. 我两次添加了程序集。

  • Once in the hibernate.cfg.xml : <mapping assembly="WebApplication1"/> 一旦进入hibernate.cfg.xml<mapping assembly="WebApplication1"/>
  • and once when building the session factory: configuration.AddAssembly(Assembly.GetCallingAssembly()); 并且在构建会话工厂时一次: configuration.AddAssembly(Assembly.GetCallingAssembly());

I now deleted the one in the hibernate.cfg.xml 我现在在hibernate.cfg.xml删除了一个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM