无法为NHibernate加载自定义ADO.NET提供程序

时间:2014-01-31 14:17:38

标签: c# postgresql nhibernate devart dotconnect

我使用Devart Postgres驱动程序作为NHibernate的Ado.net提供程序。由于NHibernate不支持Devart Postgres驱动程序,因此我编写了一个基于ReflectionBasedDriver的自定义驱动程序类。这是代码:

namespace PostgresDriver.DbDriver
{
    class DevartPgDriver : ReflectionBasedDriver
    {
        public DevartPgDriver()
            : base(
            "Devart.Data.PostgreSql",
            "Devart.Data.PostgreSql.PgSqlConnection",
            "Devart.Data.PostgreSql.PgSqlCommand")
        {
        }

        public override string NamedPrefix
        {
            get { return ":"; }
        }

        public override bool UseNamedPrefixInParameter
        {
            get { return true; }
        }

        public override bool UseNamedPrefixInSql
        {
            get { return true; }
        }

        public override bool SupportsMultipleOpenReaders
        {
            get { return false; }
        }

        protected override bool SupportsPreparingCommands
        {
            get { return true; }
        }

        public override IResultSetsCommand GetResultSetsCommand(NHibernate.Engine.ISessionImplementor session)
        {
            return new BasicResultSetsCommand(session);
        }

        public override bool SupportsMultipleQueries
        {
            get { return true; }
        }

        protected override void InitializeParameter(IDbDataParameter dbParam, string name, NHibernate.SqlTypes.SqlType sqlType)
        {
            base.InitializeParameter(dbParam, name, sqlType);

            // Since the .NET currency type has 4 decimal places, we use a decimal type in PostgreSQL instead of its native 2 decimal currency type.
            if (sqlType.DbType == DbType.Currency)
                dbParam.DbType = DbType.Decimal;
        }
    }
}

我在我的解决方案中添加了Devart.Data和Devart.Data.PostgreSql DLL作为参考,并将“Copy Local”属性设置为True。我还在App.Config中添加了以下部分:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <qualifyAssembly partialName="Devart.Data.PostgreSql"
            fullName="Devart.Data.PostgreSql, Version=7.2.80.0, Culture=neutral, PublicKeyToken=09af7300eec23701">
      </qualifyAssembly>
    </assemblyBinding>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <qualifyAssembly partialName="Devart.Data"
            fullName="Devart.Data, Version=5.0.872.0, Culture=neutral, PublicKeyToken=09af7300eec23701">
      </qualifyAssembly>
    </assemblyBinding>
  </runtime>

这是我的hibernate.cfg.xml:

<session-factory name="NHSessionFactory">
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">PostgresDriver.DbDriver.DevartPgDriver, Devart.Data.PostgreSql</property>
    <property name="dialect">NHibernate.Dialect.PostgreSQL82Dialect</property>
    <property name="connection.connection_string">User Id=***;Password=***;Host=localhost;Port=5433;Database=***;</property>
    <property name="show_sql">true</property>    
    <property name="format_sql">true</property
  </session-factory>

当我致电sessionFactory = cfg.BuildSessionFactory();时,我收到以下错误:"Could not load type 'PostgresDriver.DbDriver.DevartPgDriver' from assembly 'Devart.Data.PostgreSql, Version=7.2.80.0, Culture=neutral, PublicKeyToken=09af7300eec23701'."

当我尝试实例化驱动程序类DevartPgDriver dr = new DevartPgDriver();时,我在静态成员下得到错误:

NHibernate.Driver.ReflectionBasedDriver.ReflectionTypedProviderExceptionMessageTemplate
"The IDbCommand and IDbConnection implementation in the assembly {0} could not be found. Ensure that the assembly {0} is located in the application directory or in the Global Assembly Cache. If the assembly is in the GAC, use <qualifyAssembly/> element in the application configuration file to specify the full name of the assembly."

我错过了什么?我已经解决了这个问题数小时而没有太大的成功。请帮忙!

2 个答案:

答案 0 :(得分:0)

经过几个小时的反复试验,我能够解决问题:

而不是:

public DevartPgDriver()
        : base(
        "Devart.Data.PostgreSql",
        "Devart.Data.PostgreSql.PgSqlConnection",
        "Devart.Data.PostgreSql.PgSqlCommand")
    {
    }

应该是:

public DevartPgDriver()
        : base(
        "Devart.Data.PostgreSql",
        "Devart.Data.PostgreSql",
        "PgSqlConnection",
        "PgSqlCommand")
    {
    }

我还在nHibernate配置中添加了nhProperties.Add(NHibernate.Cfg.Environment.Hbm2ddlKeyWords, "none");。感谢Rippo的指导。

答案 1 :(得分:0)

我们在Devart论坛上回答了您:http://forums.devart.com/viewtopic.php?f=3&t=28844