[英]Create new database using Entity Framework Core, npgsql, PostgreSQL
[英]Connection Refused error on Postgres database creation using Npgsql Entity Framework Core
我正在嘗試通過我的 .NET 核心應用程序創建和使用 PostgreSQL 數據庫。 我正在使用 Npgsql 提供的 Entity Framework Core,並且我將采用“代碼優先”的方法。 但是,當我嘗試生成數據庫(使用遷移或“Database.EnsureCreated()”方法)時,我總是得到同樣的錯誤:
Npgsql.NpgsqlException(0x80004005):連接時出現異常---> System.Net.Sockets.SocketException(10061):連接被拒絕。
我在 Windows 和 Linux 上都試過了,我得到了相同的結果。
我為我的項目添加的包是:
我的項目使用 .NET Core 3.1
這是我的代碼:
using Microsoft.EntityFrameworkCore;
using System;
namespace PostgresTest {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hello World!");
}
}
public class PeopleContext : DbContext {
public DbSet<Person> People { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder options)
=> options.UseNpgsql("Host=localhost;Database=postgres;Port=5432;Username=postgres;Password=12345678");
}
public class Person {
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
編輯:需要明確的是,當我使用遷移時,創建遷移本身會成功,但是當我之后使用“數據庫更新”命令時,我仍然會收到上述錯誤。
從https://www.enterprisedb.com/downloads/postgres-postgresql-downloads下載 PostgreSQL 服務器並根據您的配置更新您的連接字符串。
更多https://code-maze.com/configure-postgresql-ef-core/
我覺得你的連接不好。 嘗試通過SERVER
更改HOST
protected override void OnConfiguring(DbContextOptionsBuilder options)=>
//options.UseNpgsql("Host=localhost;Database=postgres;Port=5432;Username=postgres;Password=12345678");
options.UseNpgsql("Server=localhost;Database=postgres;Port=5432;Username=postgres;Password=12345678");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.