Unable to load dotConnect ADO.NET provider for NHibernate

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
aparajithanv
Posts: 1
Joined: Fri 31 Jan 2014 14:19

Unable to load dotConnect ADO.NET provider for NHibernate

Post by aparajithanv » Fri 31 Jan 2014 14:23

I am using Devart Postgres driver as the Ado.net provider with NHibernate. Since NHibernate does not support Devart Postgres driver, I wrote a custom driver class based on ReflectionBasedDriver. Here is the code:

Code: Select all

 namespace PostgresDriver.DbDriver
{
    class DevartPgDriver : ReflectionBasedDriver
    {
        public DevartPgDriver()
            : base(
            //"Devart.Data.PostgreSql",
            "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;
        }
    }
}
I have added Devart.Data and Devart.Data.PostgreSql DLLs as references in my solution and set 'Copy Local' property to True. I have also added the following section in App.Config:

Code: Select all

      <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>
Here is my hibernate.cfg.xml:

Code: Select all

    <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>
When I call

Code: Select all

sessionFactory = cfg.BuildSessionFactory();
I get the following error:

Code: Select all

"Could not load type 'PostgresDriver.DbDriver.DevartPgDriver' from assembly 'Devart.Data.PostgreSql, Version=7.2.80.0, Culture=neutral, PublicKeyToken=09af7300eec23701'."
When I try to instantiate the driver class `DevartPgDriver dr = new DevartPgDriver();` I get the error under the static members:

Code: Select all

    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."
What am I missing? I have troubleshooted this issue for hours without much success. Please help! :(

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Unable to load dotConnect ADO.NET provider for NHibernate

Post by Shalex » Mon 03 Feb 2014 16:56

Please perform these steps:

1. Install the Professional edition of dotConnect for PostgreSQL, because only Professional includes the NHibernate runtime support: http://www.devart.com/dotconnect/postgr ... tions.html.
aparajithanv wrote:"The IDbCommand and IDbConnection implementation in the assembly {0} could not be found [..]"
This error says that your current edition of dotConnect for PostgreSQL is not Professional. You can check this via Tools > PostgreSQL > About menu of Visual Studio.

2. Replace

Code: Select all

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

Code: Select all

        public DevartPgDriver()
            : base(
            "Devart.Data.PostgreSql",
            "Devart.Data.PostgreSql.NHibernate.NHibernatePgSqlConnection",
            "Devart.Data.PostgreSql.NHibernate.NHibernatePgSqlCommand")
      {
      }
in your DevartPgDriver class.

3. Replace

Code: Select all

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

Code: Select all

        public override bool UseNamedPrefixInParameter
        {
            get { return false; }
        }
in your DevartPgDriver class.

4. The "Could not load type 'PostgresDriver.DbDriver.DevartPgDriver' from assembly 'Devart.Data.PostgreSql, [...]" error is caused by this setting:
aparajithanv wrote:Here is my hibernate.cfg.xml:

Code: Select all

[...]
<property name="connection.driver_class">PostgresDriver.DbDriver.DevartPgDriver, Devart.Data.PostgreSql</property>
[...]
The Devart.Data.PostgreSql.dll assembly doesn't include the DevartPgDriver class. It is defined in your code, so you should specify the name of your assembly where the DevartPgDriver class resides:

Code: Select all

<property name="connection.driver_class">PostgresDriver.DbDriver.DevartPgDriver, YourAssemblyNameHere</property>
5. After this, you should be able to execute the following code:

Code: Select all

    Configuration cfg = new Configuration().AddAssembly(typeof(Program).Assembly); //xml mapping
    ISessionFactory sessionFactory = cfg.BuildSessionFactory(); // xml mapping

    ISession session = sessionFactory.OpenSession();

    IQuery query = session.CreateQuery("from MyMappedClass i");
    IList res = query.List();
We have just sent a test application to the e-mail address you specified in your forum profile. For creating the *.hbml model (which allows generating a code and mapping automatically) in the project, we used Entity Developer for NHibernate.

shubham15
Posts: 6
Joined: Thu 27 Jan 2022 07:34

Re: Unable to load dotConnect ADO.NET provider for NHibernate

Post by shubham15 » Thu 27 Jan 2022 12:22

So now do we have any driver created to connect ADO.NET provider for nhibernate?

Shalex
Site Admin
Posts: 9543
Joined: Thu 14 Aug 2008 12:44

Re: Unable to load dotConnect ADO.NET provider for NHibernate

Post by Shalex » Fri 28 Jan 2022 14:37

The name of the provider is dotConnect for PostgreSQL Professional Edition. Please follow this walkthrough: viewtopic.php?t=28844#p98534.

Post Reply