NHibernate,对象引用(配置)未设置为对象的实例
NHibernate, Object reference(configuration) not set to an instance of an object
我有一个从其他方法调用的方法。此方法创建一个配置对象和一个 ISessionFactory:
private static ISessionFactory sessionFactory()
{
Configuration myConfig = null;
ISessionFactory mySessFac = null;
try
{
myConfig = new Configuration();
if (myConfig != null)
{
myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
mySessFac = myConfig.BuildSessionFactory();
}
}
catch (Exception e)
{
throw;
}
return mySessFac;
}
问题是我在 myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
处遇到错误
错误是:
Object reference not set to an instance of an object
尽管这是一个直截了当的错误,但我很难解决它。
下面是一个使用上述方法的方法:
public static IList<Course> RetrieveAllCourses()
{
IList<Course> cList = null;
try
{
using (ISession mySess = sessionFactory().OpenSession())
{
ICriteria criteria = mySess.CreateCriteria<Course>();
cList = criteria.List<Course>();
}
}
catch (Exception e)
{
throw;
}
return cList;
}
我的hibernate.cfg.xml设置为嵌入资源
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=Mnemonics;User ID=Mnmncs;Password=mnmncs;Initial Catalog=database-name;Integrated Security=true</property>
<property name="show_sql">true</property>
<mapping assembly="RManageSystemService"/>
</session-factory>
</hibernate-configuration>
并且映射文件也设置为嵌入资源:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="RManageSystemService"
namespace="RManageSystemService.orm_rman_systm">
<class name="Course" table="dbo.Courses" lazy="false">
<id name="CCode" column="ccode">
<generator class="identity"/>
</id>
<property name="CName" column="cname"/>
<property name="Credits" column="credits"/>
</class>
</hibernate-mapping>
那么我的代码有什么问题呢?我会很感激一些帮助。
我认为问题可能出在路径或 XML 结构中,但我对 NHibernate 框架没有经验,所以我不确定 Configure() 方法的功能。
问题是其中一个映射文件重复了 属性 映射。该错误以某种方式被方法 sessionFactory() 中的 try catch 语句覆盖,该方法仅返回一个对象错误。
我有一个从其他方法调用的方法。此方法创建一个配置对象和一个 ISessionFactory:
private static ISessionFactory sessionFactory()
{
Configuration myConfig = null;
ISessionFactory mySessFac = null;
try
{
myConfig = new Configuration();
if (myConfig != null)
{
myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
mySessFac = myConfig.BuildSessionFactory();
}
}
catch (Exception e)
{
throw;
}
return mySessFac;
}
问题是我在 myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
错误是:
Object reference not set to an instance of an object
尽管这是一个直截了当的错误,但我很难解决它。 下面是一个使用上述方法的方法:
public static IList<Course> RetrieveAllCourses()
{
IList<Course> cList = null;
try
{
using (ISession mySess = sessionFactory().OpenSession())
{
ICriteria criteria = mySess.CreateCriteria<Course>();
cList = criteria.List<Course>();
}
}
catch (Exception e)
{
throw;
}
return cList;
}
我的hibernate.cfg.xml设置为嵌入资源
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=Mnemonics;User ID=Mnmncs;Password=mnmncs;Initial Catalog=database-name;Integrated Security=true</property>
<property name="show_sql">true</property>
<mapping assembly="RManageSystemService"/>
</session-factory>
</hibernate-configuration>
并且映射文件也设置为嵌入资源:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="RManageSystemService"
namespace="RManageSystemService.orm_rman_systm">
<class name="Course" table="dbo.Courses" lazy="false">
<id name="CCode" column="ccode">
<generator class="identity"/>
</id>
<property name="CName" column="cname"/>
<property name="Credits" column="credits"/>
</class>
</hibernate-mapping>
那么我的代码有什么问题呢?我会很感激一些帮助。
我认为问题可能出在路径或 XML 结构中,但我对 NHibernate 框架没有经验,所以我不确定 Configure() 方法的功能。
问题是其中一个映射文件重复了 属性 映射。该错误以某种方式被方法 sessionFactory() 中的 try catch 语句覆盖,该方法仅返回一个对象错误。