如何在asp.net mvc core中的program.cs中添加pgadmin db

问题描述 投票:0回答:3

builder.Services.AddDbContext(选项=>        options.UseSQLserver(builder.Configuration.GetConnectionString(“数据库”)));

如何为 pgadmin 4 添加连接字符串,上面一个是针对 sqlserver 的

c# asp.net-mvc asp.net-core asp.net-web-api pgadmin-4
3个回答
1
投票

对于 postgres,你可以像这样添加

services.AddDbContext<PostgreSqlContext>(options =>
        options.UseNpgsql(Configuration.GetConnectionString("Database")));

了解更多可以关注thisthis


1
投票

pgadmin4
不是数据库,请问如何连接PostgreSQL?

首先需要添加NuGet包:

dotnet add package Npgsql.EntityFrameworkCore.PostgreSQL

然后,你需要在

connectionstring
中添加
appsettings.json

{
    "ConnectionStrings": {
        "WebApiDatabase": "Host=localhost; Database=xxx; Username=xxx; Password=xxx"
    },
}

最后这样配置:

services.AddDbContext<xxxContext>(options =>
        options.UseNpgsql(Configuration.GetConnectionString("ConnectionStrings")));

0
投票

对于 Program.cs

builder.Services.AddEntityFrameworkNpgsql().AddDbContext<ApiDbContext>(opt =>
        opt.UseNpgsql(builder.Configuration.GetConnectionString("SampleDbConnection")));
© www.soinside.com 2019 - 2024. All rights reserved.