[SOLVED] How do I resolve this error in .NET Minimal API integration with PostgreSQL?

Issue

I am trying to make APIs using ASP.Net Minimal API thingy and I am trying to use PostgreSQL as Database so for that I am following an Article and and I have followed it so far but I am unable to get desired output.

I have provided all the files below from my Project…

Program.cs

using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

builder.Services.AddDbContext<BookStoreDB>(options => options.UseNpgsql(connectionString));

builder.Services.AddDatabaseDeveloperPageExceptionFilter();

var app = builder.Build();

app.MapGet("/", () =>
{
    return "Hello World";
});

app.MapPost("/register", (BookStoreDB database, RegisterRequest body, HttpResponse response) =>
{
    User user = new User(1);
    user.FirstName = body.firstName;
    user.LastName = body.lastName;
    user.Email = body.email;
    user.Password = body.password;
    database.Users.Add(user);
    database.SaveChanges();
    response.StatusCode = 201;
    return new { requestBody = body };
});

appsettings.json

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=localhost;Port=5432;Database=BookStore;User=postgres;Password=somepass"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}

BookStoreDB.cs

using Microsoft.EntityFrameworkCore;

public class BookStoreDB : DbContext
{
    public BookStoreDB(DbContextOptions<BookStoreDB> options) : base(options)
    {

    }

    public DbSet<User> Users => Set<User>();
}

RequestObjects.cs

public class LoginRequest
{
    public string email { get; set; } = default!;
    public string password { get; set; } = default!;
}

public class RegisterRequest
{
    public string firstName { get; set; } = default!;
    public string lastName { get; set; } = default!;
    public string email { get; set; } = default!;
    public string password { get; set; } = default!;
}

User.cs

public record User(int id)
{
    public string FirstName { get; set; } = default!;
    public string LastName { get; set; } = default!;
    public string Email { get; set; } = default!;
    public string Password { get; set; } = default!;
}

I don’t have any Idea what these files are for and the convention about them. But when I run the Project using dotnet watch run it starts successfully but whenever I try to make an POST request on /register route , I get an Error shown below…

enter image description here

Solution

The "user" parameter is not able to be set because of the error in the connection string provided. Update the connection string as follows in appsettings.json file:

 "ConnectionStrings": { 
"DefaultConnection":"User ID=postgres;Password=somepass;Host=localhost;Port=5432;Database=BookStore;CommandTimeout=100"
}

You can ignore the CommandTimeout if not needed.

Answered By – ScareCrow

Answer Checked By – Gilberto Lyons (BugsFixing Admin)

Leave a Reply

Your email address will not be published.