postgresqlentity-frameworkdocker-composeasp.net-core-3.0

How to fix the problem : "System.InvalidOperationException: An exception has been raised that is likely due to a transient failure."


Let me explain : I have a .net core 3.0 API connected to a postgres database. I would like to perform basic CRUD operations with this API. I use swagger to display webrequest. Once I have created my context I migrate to the database which works very well however, as soon as I want to interact with my created table I have these errors that appears :

ExtendedSocketException : Cannot assign requested address [::1]:5433

![NpgsqlException : Exception while connecting ]2

The application is based on a docker container. (docker compose). Containers

Connection to the database :

ConnectionString

Connection

My controller :

[Route("api/[controller]")]
[ApiController]
public class CatalogueController : ControllerBase
{
    private readonly CatalogueContext _context;

    public CatalogueController(CatalogueContext context)
    {
        _context = context;
    }

    [HttpGet]
    public ActionResult<IEnumerable<Article>> GetCatalogue()
    {
        return _context.Article.ToList();
    }
}

My Context :

  public class CatalogueContext : DbContext
{
    public CatalogueContext(DbContextOptions<CatalogueContext> options) : base(options)
    {
    }
    public DbSet<Article> Article { get; set; }
}

The migration that works: migration

My Table Article on my database (I use DBeaver) :

Article


Solution

  • Migrating OP's solution from a comment to an answer:

    I had to change Server = localhost to Server = host.docker.internal.
    – Dyn amo   Jan 14, 2020 at 14:46