1
votes

I am trying to create and use a PostgreSQL database with my .NET Core application. I'm using Entity Framework Core provided by Npgsql, and I'm going for the 'code-first' approach. However, when I try to generate the database (using either migrations or the "Database.EnsureCreated()" method) I always get the same error:

Npgsql.NpgsqlException (0x80004005): Exception while connecting ---> System.Net.Sockets.SocketException (10061): Connection refused.

I have tried both on Windows and Linux, and I've gotten the same result.

The packages I've added for my project are:

  • Npgsql.EntityFrameworkCore.PostgreSQL version 3.1.3
  • Microsoft.EntityFrameworkCore.Design version 3.1.3

My project uses .NET Core 3.1

Here's my code:

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; }
    }
}

Edit: To be clear, when I use migrations, creating the migration itself succeeds, but I will still get the error mentioned above when I use the 'database update' command afterwards.

2
you might be using wrong connection string. found this medium.com/faun/…Manish
@ManishTiwari I tried changing the connection string to what the article you linked to showed, but no luck. I still get the same error messageMcFlyboy
migration is working because it will check the old snapshot and create new migration (not using DB), but when you run update DB command it will try to connect to DB, so I think your connection string might be wrong. can you check the port, user name and password? or try to connect directly using some tool (like SSMS for SQL server)Manish
is there any inner exception available?Manish
yup code first means it will create the DB but it will run the SQL queries when you run Update database command. can you try to use this (pgadmin.org) tool and use credentials you are using? (this is only to check whether you are using correct credentials or not )Manish

2 Answers

1
votes

download PostgreSQL server from https://www.enterprisedb.com/downloads/postgres-postgresql-downloads and update your connection string as per your configuration.
for more https://code-maze.com/configure-postgresql-ef-core/

0
votes

I think your connection is bad. try to change HOST by SERVER

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");