概述:
当运行 session.Save(tnp);时,出现这个"Unknown entity class: TestCleanSnow.TestNhibernatePerson"这个异常.
映射文件 TestNhibernatePerson.hbm.xml如下:
<?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>
<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>
操作代码如下:
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();
}
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如下:
<configuration>
<!-- Add this element -->
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<!--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />-->
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="dialect">NHibernate.Dialect.OracleDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">user id=jkpt;data source=jkorasvr;password=designer;</property>
<property name="connection.isolation" >ReadCommitted</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping assembly="TestCleanSnow" />
</session-factory>
</hibernate-configuration>
<!-- Add this element -->
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<!--<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />-->
</configSections>
<!-- Add this element -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="dialect">NHibernate.Dialect.OracleDialect</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">user id=jkpt;data source=jkorasvr;password=designer;</property>
<property name="connection.isolation" >ReadCommitted</property>
<property name="show_sql">true</property>
<!-- mapping files -->
<mapping assembly="TestCleanSnow" />
</session-factory>
</hibernate-configuration>
说明:在操作代码中加载相应引用后 config.AddAssembly("TestCleanSnow");,就不出现错误.
如果以配置文件的形式加载引用却出现如题的错误.
解决方法1:
可能是相应的映射文件没有设置成"嵌入式资源".我这里相应的映射文件为 TestNhibernatePerson.hbm.xml.
在解决方案资源管理器中找到TestNhibernatePerson.hbm.xml该映射文件
右击---属性----在将“生成操作”设置成“嵌入的资源”.
解决方法1我已经设置成"嵌入入的资源",但还是出现如题的错误,请问哪位大侠已经解决了,给予指示.