.net 使用 PostgreSQL 的 Linq To Sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4855095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:11:50  来源:igfitidea点击:

Linq To Sql with PostgreSQL

.netlinqlinq-to-sqlpostgresqlmono

提问by Chernikov

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

是否可以将 LinqToSql 与 PostgreSQL(最好与 Mono)一起使用?你能推荐任何文章一步一步地描述吗?

采纳答案by Randy Minder

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

不是开箱即用的。您必须找到为 PostgreSQL 编写提供程序的第三方。

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

DbLinq就是这样的提供者之一。可以在此处找到安装和使用说明。

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

请注意,截至 2016 年,DbLinq 项目已移至github,已存档且不再维护。

回答by CedX

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

我使用 LINQ to SQL 大约 2 年以来使用 MySQL 和 PostgreSQL 数据库(在 Windows 上使用 DbLinq,在 Linux 和 Mac OS X 上使用 Mono)。

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

所以 LINQ to SQL 不仅限于 SQL Server,但如上所述,您需要使用外部库。

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.

在 Mono 上,DbLinq 是 System.Data.Linq 命名空间的基础:您不需要部署/注册 DbLinq 程序集。Npgsql 程序集的同上:它已由最新版本的 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).

您应该知道支持是不完整的(当我尝试将 ObjectDataSource 控件绑定到 PostgreSQL 数据上下文时遇到了很多问题:我通过对生成的实体进行手动数据绑定来解决这些问题)。但这对于大多数 SQL 查询来说已经足够了(我几乎从未遇到过任何失败的查询:复杂的关节可以通过几个更简单的查询来模拟)。

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

DbLinq 和 LINQ to SQL 之间几乎没有区别。只有连接字符串必须通过提供有关数据提供程序的信息进行自定义。查看这些简短的示例(我从未找到任何有关 DbLinq 使用的真正教程):

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

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"

就个人而言,对于 Mono 2.10.6,我使用这种类型的连接字符串:“Server=localhost; Database=MyDB; User Id=postgres; Password=MyPassword; DbLinqProvider=PostgreSql; DbLinqConnectionType=Npgsql.NpgsqlConnection, Npgsql, Version=4.0。 0.0,文化=中性,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).

注意:在 Mono 上,DbMetal 工具被替换为“sqlmetal”命令,该命令具有相同的参数(它只是 DbMetal 的包装器)。

回答by Oded

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

此处有用于 postgres(以及 MySql 和其他数据库)的第三方 Linq 提供程序。

Another option is ngpsql
UPDATEThe npgsqllibrary has moved to Github here is an updated link

另一种选择是ngpsql
UPDATENpgsql的这里库已经转移到Github上是一个更新的链接

回答by Pierre

Use LinqToDB for PostgreSQL

使用 LinqToDB for PostgreSQL

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

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

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

在 Visual Studio 中,打开 NuGet 控制台并运行以下命令:

PM> Install-Package linq2db.PostgreSQL

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.

您最终将在解决方案中得到一个文件夹:LinqToDB.PostgreSQL. 在该文件夹中有一个名为CopyMe.PostgreSQL.tt.txt. 在文件中阅读如何生成 Context 类。

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 *.ttfile, the class will be regenerated. Works well for our company of 21 developers.

每次保存*.tt文件时,都会重新生成该类。非常适合我们拥有 21 名开发人员的公司。

回答by Darin Dimitrov

Linq-to-SQLsupports only SQL Server. Entity Frameworksupports other databases as well.

Linq-to-SQL仅支持 SQL Server。实体框架也支持其他数据库。

回答by HymanD

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.

不可以,LINQ to SQL 仅适用于 MS SQL Server。您必须使用 3-rd 方提供程序来访问 PostgreSQL 数据源。我建议您尝试Devart LinqConnect。此外,该产品还提供了 Entity Framework 的一些功能,例如 - 复杂类型、差异继承类型支持等。