【发布时间】:2014-02-23 18:42:49
【问题描述】:
我正在使用 NHibernate 制作一个简单的 Web 项目,但每当我尝试构建 sessionfactory 时都会遇到此错误。
导致异常的行是这个
ISessionFactory sessionFactory = new Configuration().Configure().BuildSessionFactory();
有类似问题的人似乎通过引用我已经完成的 Mysql.data.dll 来解决它们,并检查该 dll 是否在我的 bin 文件夹中。
我怀疑问题出在我的 hibernate.cfg.xml 中,看起来像这样
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.MySQLDialect</property>
<property name="connection.provider">NHibernate.Driver.MySqlDataDriver</property>
<property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
<property name="connection.connection_string">connectionstring</property>
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
<mapping assembly="Mcgvd" />
</session-factory>
</hibernate-configuration>
我在制作这个项目时遵循的教程是使用帮助类来创建看起来像这样的会话工厂
public sealed class NHibernateHelper
{
private const string CurrentSessionKey = "nhibernate.current_session";
private static readonly ISessionFactory sessionFactory;
static NHibernateHelper()
{
sessionFactory = new Configuration().Configure().BuildSessionFactory();
}
public static ISession GetCurrentSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
currentSession = sessionFactory.OpenSession();
context.Items[CurrentSessionKey] = currentSession;
}
return currentSession;
}
public static void CloseSession()
{
HttpContext context = HttpContext.Current;
ISession currentSession = context.Items[CurrentSessionKey] as ISession;
if (currentSession == null)
{
// No current session
return;
}
currentSession.Close();
context.Items.Remove(CurrentSessionKey);
}
public static void CloseSessionFactory()
{
if (sessionFactory != null)
{
sessionFactory.Close();
}
}
}
hibernate.cfg.xml 和 hibernate.hbm.xml 都位于我项目的根目录。
我在这里做错了什么?
【问题讨论】:
标签: c# mysql asp.net-mvc nhibernate