【问题标题】:.NET nHibernate: resource file not found.NET nHibernate:找不到资源文件
【发布时间】:2023-04-07 04:18:01
【问题描述】:

我似乎无法让我的 nHibernate 测试项目运行,我正在使用以下配置文件和代码:

country.hbm.xml 标记为嵌入资源:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="SVL.Models.CountryModel, SVL" table="country">
  <id name="Id" type="int" />
  <property name="Name" type="String" length="200" />
 </class>
</hibernate-mapping>

我的休眠配置文件:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<!-- properties -->
<property name="connection.provider">
  NHibernate.Connection.DriverConnectionProvider
</property>
<property name="connection.driver_class">
  NHibernate.Driver.MySqlDataDriver
</property>
<property name="connection.connection_string">
  Server=localhost;Database=svl;User ID=root;Password=pfje1008;
</property>
<property name="dialect">
  NHibernate.Dialect.MySQL5Dialect
</property>

<mapping resource="country.hbm.xml" assembly="SVL" />
</session-factory>
</hibernate-configuration>`

最后是设置休眠配置的代码:

var cfg = new Configuration();
 cfg.Configure();

 var sessionFactory = cfg.BuildSessionFactory();

 var thisAssembly = typeof(T).Assembly;
 cfg.AddAssembly(thisAssembly);

由于某种原因,它一直告诉我找不到资源文件...

【问题讨论】:

  • 异常的堆栈跟踪是什么?
  • [MappingException: Resource not found: country.hbm.xml] NHibernate.Cfg.Configuration.LogAndThrow(异常异常)+131 NHibernate.Cfg.Configuration.AddResource(字符串路径,程序集程序集)+208 NHibernate.Cfg.Configuration.DoConfigure(ISessionFactoryConfiguration factoryConfiguration) +936 NHibernate.Cfg.Configuration.Configure(XmlReader textReader) +327 NHibernate.Cfg.Configuration.Configure(String fileName, Boolean ignoreSessionFactoryConfig) +166
  • 如果我在我的配置中硬编码映射配置文件,那么它确实可以工作:cfg.AddFile(HttpContext.Current.Server.MapPath("country.hbm.xml"));
  • 看来您应该将程序集名称添加到映射中.. 使用 ´´ 确实有效,叹息..

标签: .net nhibernate


【解决方案1】:

尝试去掉 &lt;mapping resource="country.hbm.xml" assembly="SVL" /&gt; 行,我不记得在使用嵌入式资源时做过。

【讨论】:

  • 当我这样做时,我没有收到错误,但也没有收到任何返回值。如果我确实添加了 'cfg.AddFile(HttpContext.Current.Server.MapPath("country.hbm.xml"));' 中的配置,我得到了当前在数据库中的 3 条记录..
  • 如果不添加映射文件,那么如何进行映射呢?
  • 我承认我已经有一段时间没有使用 NHibernate 了,虽然我使用嵌入式资源并且我确定我没有单独列出它们,我只是添加了它们嵌入的程序集:cfg.AddAssembly(typeof(Customer).Assembly);
【解决方案2】:

有几个问题,首先,您调用的 .Configure 方法正在寻找一个包含连接字符串、方言和其他设置的文件(或者可能检查您的 web.config 以了解这些),其次是您在将程序集添加到配置之前重新构建会话工厂。

您需要在构建会话工厂之前将程序集添加到配置中,因为构建会话工厂的过程基于配置的当前状态。

比如:

var cfg = new Configuration();
var thisAssembly = typeof(T).Assembly;
cfg.AddAssembly(thisAssembly); 
cfg.Configure();

var sessionFactory = cfg.BuildSessionFactory();

要解决您遇到的异常,我建议:

  1. 将您的 NHibernate 配置文件重命名为 hibernate.cfg.xml(如果在 app/web.config 中找不到它,则默认情况下会查找它)
  2. 将配置放入您的 app/web.config(请参阅http://nhforge.org/blogs/nhibernate/archive/2009/07/17/nhibernate-configuration.aspx
  3. 在对 Configure 的调用中指定您的 NHibernate 配置文件的名称 - 即。 cfg.Configure("my-nhibernate-config.config")

【讨论】:

    猜你喜欢
    • 2018-05-16
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 2020-03-08
    • 2012-02-04
    • 2012-05-16
    相关资源
    最近更新 更多