Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq To Sql with PostgreSQL

Is it possible to use LinqToSql with PostgreSQL (preferably with Mono)? Can you recommend any articles where it described step-by-step?

like image 629
Chernikov Avatar asked Jan 31 '11 19:01

Chernikov


People also ask

Does Linq work with PostgreSQL?

No, LINQ to SQL works only with MS SQL Server. You have to use 3-rd party provider to access PostgreSQL datasource.

Can you use SQL with Postgres?

PostgreSQL is a powerful, open source object-relational database system that uses and extends the SQL language combined with many features that safely store and scale the most complicated data workloads.


2 Answers

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).

like image 88
CedX Avatar answered Oct 06 '22 13:10

CedX


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.

like image 20
Randy Minder Avatar answered Oct 06 '22 13:10

Randy Minder