Chernikov
Chernikov

Reputation: 857

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?

Upvotes: 30

Views: 30626

Answers (6)

Oded
Oded

Reputation: 498914

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

Upvotes: 10

Randy Minder
Randy Minder

Reputation: 48402

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.

Upvotes: 13

Pierre
Pierre

Reputation: 9052

Use LinqToDB for PostgreSQL

https://www.nuget.org/packages/linq2db.PostgreSQL/

In Visual studio, open NuGet console and run the following:

PM> Install-Package linq2db.PostgreSQL

You will end up with a folder in your solution: LinqToDB.PostgreSQL. Within the folder there is a file called CopyMe.PostgreSQL.tt.txt. Read in the file what to do to get your Context class generated.

Example of this file contents:

<#@ template language="C#" debug="True" hostSpecific="True"                            #>
<#@ output extension=".generated.cs"                                                   #>
<#@ include file="$(ProjectDir)LinqToDB.Templates\LinqToDB.PostgreSQL.Tools.ttinclude" #>
<#@ include file="$(ProjectDir)LinqToDB.Templates\PluralizationService.ttinclude"      #>
<#
    /*
        1. Copy this file to a folder where you would like to generate your data model,
           rename it, and delete .txt extension. For example:

            MyProject
                DataModels
                    MyDatabase.tt

        2. Modify the connection settings below to connect to your database.

        3. Add connection string to the web/app.config file:

            <connectionStrings>
                <add name="MyDatabase" connectionString="Server=MyServer;Port=5432;Database=MyDatabase;User Id=postgres;Password=TestPassword;Pooling=true;MinPoolSize=10;MaxPoolSize=100;Protocol=3;" providerName="PostgreSQL" />
            </connectionStrings>

        4. To access your database use the following code:

            using (var db = new MyDatabaseDB())
            {
                var q =
                    from c in db.Customers
                    select c;

                foreach (var c in q)
                    Console.WriteLine(c.ContactName);
            }

        5. See more at https://github.com/linq2db/t4models/blob/master/Templates/ReadMe.LinqToDB.md.
    */

    NamespaceName = "DataModels";

    LoadPostgreSQLMetadata("MyServer", "5432", "MyDatabase", "postgres", "TestPassword");
//    LoadPostgreSQLMetadata(string connectionString);

    GenerateModel();
#>

Everytime you save the *.tt file, the class will be regenerated. Works well for our company of 21 developers.

Upvotes: 6

CedX
CedX

Reputation: 3977

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

Upvotes: 15

JackD
JackD

Reputation: 597

No, LINQ to SQL works only with MS SQL Server. You have to use 3-rd party provider to access PostgreSQL datasource. I recommend you to try Devart LinqConnect. Also this product provides some features of Entity Framework, for example - complex type, difference inheritance type support, etc.

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

Linq-to-SQL supports only SQL Server. Entity Framework supports other databases as well.

Upvotes: 4

Related Questions