繁体   English   中英

NHibernate映射错误([EntityName]未映射)

[英]NHibernate mapping error ([EntityName] not mapped)

我的项目(c#,nhibernate,npgsql,nlog)有一个名为Entry实体,应该映射到名为test的数据库表中。 该表包含一些测试条目。

我的应用程序代码如下:

public void work()
{
    ISessionFactory sessions = new Configuration().Configure().BuildSessionFactory();
    ISession session = sessions.OpenSession();

    IList<Entry> entries = session.CreateQuery("from Entry").List<Entry>();

    foreach (Entry e in entries)
    {
        logger.Debug("Entry: " + e.id + " with " + e.name);
    }
}

Entry实体如下所示:

namespace pgsql
{
    class Entry
    {
        public virtual int id { get; set; }
        public virtual string name { get; set; }
    }
}

我的NHibernate配置的结构如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.driver_class">NHibernate.Driver.NpgsqlDriver</property>
        <property name="dialect">NHibernate.Dialect.PostgreSQLDialect</property>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="default_catalog">test</property>
        <property name="default_schema">test</property>
        <property name="show_sql">true</property>
        <property name="format_sql">true</property>
        <property name="hbm2ddl.auto">create</property>
        <property name="connection.connection_string">server=localhost;database=*****;uid=*****;pwd=*****;</property>
    </session-factory>
</hibernate-configuration>

Entry实体配置如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping schema="test" xmlns="urn:nhibernate-mapping-2.2">
    <class name="Entry" table="test">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
    </class>
</hibernate-mapping>

这引发以下错误:

条目未映射[来自条目]

我已经确定,Visual Studio hbm.xml所有hbm.xml文件复制到输出目录。 我想念什么?

根据您的答案进行编辑:

解决方案是使用Assembly进行初始化:

 ISessionFactory sessions = new Configuration().Configure().AddAssembly(Assembly.GetExecutingAssembly()).BuildSessionFactory(); 

但我仍然不知道为什么要这样。

我将解释为什么必须这样。

您的实体与应用程序在同一程序集中定义。
当您调用Configuration().Configure().BuildSessionFactory() ,实际上所有的HBM文件都将被忽略。 您需要执行以下任一操作才能将HBM文件添加到Configuration实例:

  • 对每个单独的HBM文件使用new Configuration().AddFile("MyFile.hbm.xml")
  • 使用new Configuration().Configure().AddAssembly(YourAssembly())告诉NHibernate 查找所有HBM文件并为您添加所有这些文件。
  • 下面链接的文档中提到的其他方式。

由于您既没有执行任何操作,也没有向Configuration添加任何实体Configuration 乳清您尝试运行您的HQL查询时,NHibernate正在寻找未定义的Entry实体(已添加到Configuration )。 因此是错误。

正如您在答案中提到的那样,您选择了将HBM文件添加到Configuration第二种方法,并且问题消失了。

另一种替代方法(可能是最好的方法)是让NHibernate加载Assembly中包含的所有映射文件:

 Configuration cfg = new Configuration() .AddAssembly( "NHibernate.Auction" ); 

然后,NHibernate将在程序集中查找以.hbm.xml结尾的任何资源。 这种方法消除了任何硬编码的文件名,并确保添加了程序集中的映射文件。

请参阅文档以获取更多详细信息。


原始答案

实体名称也应包括名称空间。

IList<Entry> entries = session.CreateQuery("from pgsql.Entry").List<Entry>();

为了简化重构,您还可以更改为以下内容:

var queryString = string.Format("from {0}", typeof(Entry));
IList<Entry> entries = session.CreateQuery(queryString).List<Entry>();

请参阅第14.2 docs 的from子句

这不是问题的一部分,但是,调用BuildSessionFactory的代价BuildSessionFactory 您不需要重复它。 您可以在应用程序启动时调用它,并维护ISessionFactory实例以供将来使用。

解决方案是使用Assembly进行初始化:

ISessionFactory sessions = new Configuration().Configure().AddAssembly(Assembly.GetExecutingAssembly()).BuildSessionFactory();

但我仍然不知道为什么要这样。

暂无
暂无

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

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