Linq To Sql 与 PostgreSQL

发布于 2024-10-15 10:25:43 字数 73 浏览 2 评论 0原文

是否可以将 LinqToSql 与 PostgreSQL 一起使用(最好与 Mono 一起使用)?您能推荐一些详细描述步骤的文章吗?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

女中豪杰 2024-10-22 10:25:43

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

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

在 Mono 上,DbLinq 是 System.Data.Linq 命名空间的基础:您不需要部署/注册 DbLinq 程序集。 Npgsql 程序集同上:最新版本的 Mono 已经提供了它。

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

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

就我个人而言,在 Mono 2.10.6 中,我使用这种类型的连接字符串:“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 上,DbMetal 工具被“sqlmetal”命令替换,该命令具有相同的参数(它只是 DbMetal 的包装器) )。

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

跨年 2024-10-22 10:25:43

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

DbLinq 就是此类提供商之一。您可以在此处找到安装和使用说明。

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

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.

迷离° 2024-10-22 10:25:43

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

另一个选项是 ngpsql
更新 npgsql 库已移至 Github,这里是更新的链接< /a>

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

长亭外,古道边 2024-10-22 10:25:43

使用 LinqToDB for PostgreSQL

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

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

PM>; Install-Package linq2db.PostgreSQL

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

此文件内容的示例:

<#@ 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();
#>

每次保存 *.tt 文件时,都会重新生成该类。
对于我们拥有 21 名开发人员的公司来说效果很好。

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.

萌能量女王 2024-10-22 10:25:43

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

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

你爱我像她 2024-10-22 10:25:43

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文