【问题标题】:Could not instantiate connection provider: NHibernate.Driver.MySqlDataDriver无法实例化连接提供程序:NHibernate.Driver.MySqlDataDriver
【发布时间】: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


    【解决方案1】:

    你的配置错误。

    <property name="connection.provider">NHibernate.Driver.MySqlDataDriver</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlServerCeDriver</property>
    

    您已经为连接提供程序指定了一个驱动程序,并且当您显然在使用 MySQL 时指定了一个 SQL Server CE 驱动程序。试试:

    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</property>
    

    【讨论】:

    • 啊,我回家后试试。以为我掉进了许多复制/粘贴坑之一,但不知道在哪里。
    【解决方案2】:

    除了 Jamie 发现的错误,我必须更改我出于某种原因放置 mysqldatadriver 的 connection.provider 属性。

    这是我最终得到的工作配置。

    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
    <property name="dialect">NHibernate.Dialect.MySQLDialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.MySqlDataDriver</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>
    

    【讨论】:

      猜你喜欢
      • 2020-01-17
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多