Nhibernate Unknown entity class 的解决办法
概述:
当运行 session.Save(tnp);时,出现这个"Unknown entity class: EXTSHIPFILE.TestNhibernatePerson"这个异常.
映射文件 TestNhibernatePerson.hbm.xml如下:
<pre class="html" name="code"><pre class="html" name="code"><span class="cnblogs_code_copy"></span><span class="cnblogs_code_copy"></span><span class="cnblogs_code_copy"></span><pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="TestCleanSnow" namespace="TestCleanSnow">
<class name="TestCleanSnow.TestNhibernatePerson,TestCleanSnow" table="TEST_NHIBERNATE_PERSON" lazy="false">
<id name="Usertestid" column="USERTESTID" type="Decimal">
<generator class="sequence">
<param name="sequence">emp_sequence</param>
</generator>
</id>
<property type="string" not-null="true" length="6" name="Usertestname" column="USERTESTNAME" />
</class>
</hibernate-mapping>
<span class="cnblogs_code_copy"></span>
<span class="cnblogs_code_copy"></span>
<span class="cnblogs_code_copy">操作代码:</span>
Configuration config = new Configuration();
ISessionFactory factory = config.BuildSessionFactory();
ISession session = factory.OpenSession();
TestNhibernatePerson tnp = new TestNhibernatePerson();
tnp.Usertestname = "test4";
ITransaction trans = session.BeginTransaction();
try
{
// 保存记录
session.Save(tnp);
trans.Commit();
Console.WriteLine("Insert Success!");
}
catch (Exception ex)
{
trans.Rollback();
Console.WriteLine(ex.Message);
}
finally
{
session.Close();
}
配置代码app.config如下:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
<session-factory name="JobMonitor">
<!-- properties -->
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Server=gub0-sdb03;initial catalog=B2BWS_TEST;User Id=sa;Password=123</property>
<property name="command_timeout">60</property>
<property name="show_sql">true</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
<property name="use_outer_join">true</property>
<property name="query.substitutions">true 1, false 0</property>
<!-- mapping files -->
<mapping assembly="BenQ.B2B.PlatForm.Entity"/>
</session-factory>
</hibernate-configuration>
说明:在操作代码中加载相应引用后 config.AddAssembly("TestCleanSnow");,就不出现错误.
如果以配置文件的形式加载引用却出现如题的错误.
原因分析:
问题1:
可能是相应的映射文件没有设置成"嵌入式资源".我这里相应的映射文件为 TestNhibernatePerson.hbm.xml.
在解决方案资源管理器中找到TestNhibernatePerson.hbm.xml该映射文件
右击---属性----在将“生成操作”设置成“嵌入的资源”.
问题2:
我把 Configuration config = new Configuration();改成 Configuration config = new Configuration().Configure();
让程序去找hibernate.cfg.xml,这个配置文件的配置跟app.config中的一样。
问题3:
检查映射文件映射文件 TestNhibernatePerson.hbm.xml的配置是否正确,其中
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="模块名称" namespace="命名空间">
<class name="类名" table="数据库表名" lazy="false">
<id name="Usertestid" column="USERTESTID" type="Decimal"> //主键