Linq To Sql with PostgreSQL Linq To Sql with PostgreSQL postgresql postgresql

Linq To Sql with PostgreSQL


I use LINQ to SQL since about 2 years with MySQL and PostgreSQL databases (using DbLinq on Windows, using Mono on Linux and Mac OS X).

So LINQ to SQL is NOT limited to SQL Server, but as stated above, you need to use external libraries.

On Mono, DbLinq is the foundation of the System.Data.Linq namespace: you don't need to deploy/register DbLinq assemblies. Idem for the Npgsql assembly: it is already provided by the latest versions of Mono.

You should be aware that the support is incomplete (I had numerous problems when I tried to bind an ObjectDataSource control to a PostgreSQL data context: I solved them by doing manual data binding with the generated entities). But this is enough for most SQL queries (I've almost never had any failed queries : complex joints can be simulated by several simpler queries).

There is little difference between DbLinq and LINQ to SQL. Only the connection string must be customized by supplying informations about the data provider. See these short samples (I never found any real tutorial about the use of DbLinq):

http://www.mono-project.com/Release_Notes_Mono_2.6
http://www.jprl.com/Blog/archive/development/mono/2009/Mar-12.html

Personally, with Mono 2.10.6, I use this type of connection string : "Server=localhost; Database=MyDB; User Id=postgres; Password=MyPassword; DbLinqProvider=PostgreSql; DbLinqConnectionType=Npgsql.NpgsqlConnection, Npgsql, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"

Note: on Mono, the DbMetal tool is replaced by the "sqlmetal" command, which has the same parameters (it's just a wrapper around DbMetal).


Not out of the box. You'd have to find a third party that has written a provider for PostgreSQL.

One such provider is DbLinq. Instructions for installation and use may be found here.

Please note, that as of 2016 year DbLinq project is mooved to github, archived and not maintained anymore.


There is a third party Linq provider for postgres (as well as MySql and other databases) here.

Another option is ngpsql
UPDATE The npgsql library has moved to Github here is an updated link