I am switching from SQL Server to PostgreSQL, and have a webAPI that I have updated as follows:
In the appsettings.json file:
{
"ConnectionStrings": {
"postgres": "User ID=test;Password=testing;Server=localhost;Port=5432;Database=mpau;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
And in the Program.cs file:
using Microsoft.EntityFrameworkCore;
using Npgsql;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
//added
builder.Services.AddCors(options =>
{
options.AddDefaultPolicy(
policy =>
{
policy.AllowAnyOrigin();
policy.AllowAnyMethod();
policy.AllowAnyHeader();
});
});
//added
builder.Services.AddDbContext<mpauDbContext>(options =>
{
builder.Configuration.GetConnectionString("postgres");
options.UseNpgsql("postgres");
});
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
I was able to create the data migrations, however when I try update-database, I get an error stating:
"Format of the initialization string does not conform to specification at index 0"
Most search results state that it is likely caused by an error in the connection string, but I have tried multiple changes, and I followed a tutorial where the string used was almost identical (with the exception of username and password, and I will be trying Kerberos authentication later to remove this from the connection string).
Any help in resolving this error would be greatly appreciated.