简体   繁体   English

如何在 ASP.NET 核心与 PostgreSQL 建立数据库连接?

[英]How to make database connectivity in ASP.NET core with PostgreSQL?

How to make database connection in ASP.NET core with postgres SQL?如何使用 postgres SQL 在 ASP.NET 核心中建立数据库连接?

use the Npgsql EF Core provider, add a dependency on Npgsql.EntityFrameworkCore.PostgreSQL.使用 Npgsql EF Core 提供程序,添加对 Npgsql.EntityFrameworkCore.PostgreSQL 的依赖项。 You can follow the instructions in the general EF Core Getting Started docs.您可以按照通用 EF Core 入门文档中的说明进行操作。 https://www.npgsql.org/efcore/ https://www.npgsql.org/efcore/

Question: How to make database connection in ASP.NET core with postgres SQL? Question: How to make database connection in ASP.NET core with postgres SQL?

There are two ways you add your postgresql database along with the asp.net core project.有两种方法可以将postgresql数据库与asp.net core项目一起添加。

Using follwing way:使用以下方式:

  • ADO.net connection provider
  • Nuget extension: Npgsql.EntityFrameworkCore.PostgreSQL

ADO.net connection provider

Here would just need the NpgsqlConnection connection builder class which will execute your sql on Postgre sql database server.这里只需要NpgsqlConnection连接构建器 class 它将在Postgre sql数据库服务器上执行您的sql See the example below:请参见下面的示例:

C# ASP.NET Core & Postgre SQL Ado.net example:

  NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;User Id=postgres;Password=pwd;Database=postgres;");
    
    conn.Open();
    
    // Passing PostGre SQL Function Name
    NpgsqlCommand command = new NpgsqlCommand("EXE GetEmployeePrintInfo", conn);
    
    // Execute the query and obtain a result set
    NpgsqlDataReader reader = command.ExecuteReader();
    
    // Reading from the database rows
    List<string> listOfManager = new List<string>();

    while (reader.Read())
    {
        string WSManager = reader["WSManager"].ToString(); // Remember Type Casting is required here it has to be according to database column data type
        listOfManager.Add(WSManager);
    }
    reader.Close();

    command.Dispose();
    conn.Close();

Npgsql.EntityFrameworkCore.PostgreSQL:

You have to add Nuget extension into the project reference from manage Nuget Packages using Visual studio .您必须从使用Visual studio manage Nuget PackagesNuget extension添加到项目参考中。 Entity framework core has this functionality for many database providers. Entity framework core为许多数据库提供者提供此功能。 Just follow below steps on visual studio只需在visual studio上按照以下步骤操作

在此处输入图像描述

ConfigureServices in startup.cs: Once you successfully added the Nuget package then you have to update following code on your project ConfigureServices under startup.cs ConfigureServices in startup.cs:成功添加Nuget package后,您必须在startup.cs下的项目ConfigureServices中更新以下代码

        services.AddDbContext<YourDatabaseContextClassName>(options =>
        options.UseNpgsql(Configuration.GetConnectionString("YourDatabaseContextStringNameFromAppsettings")));

Note: If you need any example for entityframework and Postgre SQL implementation you can have look here Note:如果您需要实体框架和Postgre SQL entityframework的任何示例,您可以查看此处

For further assistance you can have look on official document here .如需进一步帮助,您可以在此处查看官方文档 Hope it would guide you accordingly.希望它会相应地指导你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM