Hey Everyone..
I am new to NHibernate. I am trying to familiarize myself with it by practicing using a SQL Server Express instance running the NorthWind database.
I cannot seem to establish a connect to my database. The exception reads: "ADOException was unhandled by user code: cannot open connection"
The connection string is taken directly from the database's connection string property from within VS so I'm fairly certain it's not the issue.
Config File:
Code:
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
Helper Class (Session Factory):
Code:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Region).Assembly);
_sessionFactory = cfg.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
Query (Where Exception Occurs):
Code:
public Region GetRegionByID(int id)
{
using (ISession session = NHibernateHelper.OpenSession())
{
var region = session.Get<Region>(id);
return region;
}
}
The exception occurs at this line: var region = session.Get<Region>(id);
I sure hope someone can help.
Thanks a lot!
-Nick