You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ASP.NET MVC Core的Program.cs中配置PgAdmin数据库连接

替换为PostgreSQL(PgAdmin 4管理)数据库连接的配置方法

1. 安装PostgreSQL EF Core驱动包

首先需要安装适配PostgreSQL的EF Core NuGet包,使用以下命令之一:

  • 包管理器控制台:Install-Package Npgsql.EntityFrameworkCore.PostgreSQL
  • .NET CLI:dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL

2. 修改Program.cs中的DbContext配置

把原有的SQL Server配置替换为PostgreSQL的配置,将UseSqlServer改为UseNpgsql:

builder.Services.AddDbContext<DEM_MASTERSContext>(options => 
    options.UseNpgsql(builder.Configuration.GetConnectionString("Database")));

3. 配置PostgreSQL连接字符串

在appsettings.json(或对应环境配置文件)中,更新ConnectionStrings下的Database项,使用PostgreSQL的连接字符串格式:

"ConnectionStrings": {
  "Database": "Host=localhost;Port=5432;Database=DEM_MASTERS;Username=your_db_user;Password=your_db_password;"
}

参数说明:

  • Host:PostgreSQL服务器地址(本地用localhost,远程填对应IP/域名)
  • Port:PostgreSQL默认端口是5432,若修改过则填对应端口
  • Database:要连接的数据库名称(需提前在PgAdmin 4中创建好)
  • Username:PostgreSQL数据库的用户名(默认是postgres,建议用自定义账号)
  • Password:对应账号的密码

内容的提问来源于stack exchange,提问作者Neelima

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:57:37