[英]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 Packages
将Nuget 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 forentityframework
andPostgre SQL
implementation you can have look hereNote:
如果您需要实体框架和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.