Using PostgreSQL

One of the updates in MicroLite 3.0 was support for PostgreSQL, however there are a couple of things you need to do if you wish to use PostgreSQL which you don’t for MS SQL. This is due to the fact that the .NET framework supports MS SQL natively, whereas the PostgreSQL implementations of the System.Data classes are provided by pgfoundry.org.

The easiest way to get started is to install the Npgsql NuGet package, although you can download and install it from pgfoundry.org if you don’t/can’t use NuGet.

As usual you will still need to add a named connection string to your app.config:

<connectionStrings>
    <add name="PostgreSqlTest"
         connectionString="Server=localhost;Database=database;User Id=User;Password=Pswd;"
         providerName="Npgsql" />
</connectionStrings>

You also need to add another section to your app.config so that the .NET framework knows about the Npgsql provider:

<system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="PostgreSQL Data Provider"
           invariant="Npgsql"
           description=".Net Data Provider for PostgreSQL"
           type="Npgsql.NpgsqlFactory, Npgsql, Version=2.0.12.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />
    </DbProviderFactories>
</system.data>

Make sure that the version specified matches the version you have installed or you will get an exception thrown by the .NET runtime at startup when it tries to resolve the assembly.

Then finally, in you application startup, you need to call the overload method for ForConnection so that you can specify the PostgreSqlDialect:

var sessionFactory = Configure
    .Fluently()
    .ForConnection(connectionName: "PostgreSqlTest", sqlDialect: "MicroLite.Dialect.PostgreSqlDialect")
    .CreateSessionFactory();

This article was written for version 3.0.0 of the MicroLite framework.

Update

As of MicroLite 4.0.1, use the ForPostgreSqlConnection method instead:

var sessionFactory = Configure
    .Fluently()
    .ForPostgreSqlConnection("PostgreSqlTest")
    .CreateSessionFactory();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s