I have a method that is called from other methods. This method creates a configuration object and an 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;
}
The problem is that I get an error at myConfig.Configure(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "hibernate.cfg.xml"));
The error is:
Object reference not set to an instance of an object
Even though this is a straight forward error I have difficulties to solve it. Here is a method that uses the above method:
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;
}
My hibernate.cfg.xml is set to embedded resource
<?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>
And the mapping file is also set to embedded resource:
<?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>
So what is the problem with my code? I would appreciate some help.