NHibernate を使用するのはこれが初めてで、MVC 4 を使用してアプリケーションを作成しようとしています。ここに私の構成ファイルがあります。
<?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="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">Data Source=.\SQLEXPRESS;Initial Catalog=loc;Persist Security Info=True;Trusted_Connection=Yes;Pooling=yes;connection lifetime=300;User Id=sa;Password=###</property>
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
<property name="show_sql">false</property>
およびマッピング ファイル:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="HelloWorldHib"
namespace="HelloWorldHib.Mappings">
<class name="Product" table="Products">
<id name="Id" column="Id">
<generator class="native"></generator>
</id>
<property name="Name"></property>
<property name="Category"></property>
<property name="IsDis"></property>
</class>
</hibernate-mapping>
そしてヘルパー:
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static Configuration cfg;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
cfg.Configure(HttpContext.Current.Server.MapPath("~/hibernate.cfg.xml"));
cfg.AddDirectory(new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath(@"~/Mappings")));
cfg.AddAssembly(typeof(Product).Assembly);
_sessionFactory = cfg.BuildSessionFactory();
if (_sessionFactory == null)
throw new InvalidOperationException("session factory could not be built");
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
ISession session;
session = _sessionFactory.OpenSession();
if (session == null)
throw new InvalidOperationException("session could not be opend");
return session;
}
}
アプリを実行すると、Object reference not set to an instance of an object
この行が表示されますsession = _sessionFactory.OpenSession()
データベースにテーブルが存在します。問題は何ですか?
ありがとう