I’m making a project ASP .NET Core WEB API in .NET 8, and i can’t get the connection strings for my db to work, but if i hardcode the connection, it works just fine.
This is my try to use ConnectionStrings in Program.cs
builder.Services.AddDbContext<SISTEM_CONTEXT>(
options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
This is the error in swagger:
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
This works just fine, and the API works
builder.Services.AddDbContext<SISTEM_CONTEXT>(
options =>
{
options.UseSqlServer("server = DESKTOP-RCQQR4T,1433; database = PARTIDOS; user = joacosa; password = 1234xd;TrustServerCertificate=true;");
});
Here is my appsettings.json:
{
"ConnectionStrings": {
"DefaultConnection": "server = DESKTOP-RCQQR4T,1433; database = PARTIDOS; user = joacosa; password = 1234xd;TrustServerCertificate=true;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Full program.cs:
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using PARTIDOS_API.Data;
using PARTIDOS_API.Repository;
using PARTIDOS_API.Repository.Interfaces;
using PARTIDOS_API.REPOSITORY;
using PARTIDOS_API.REPOSITORY.Interfaces;
using PARTIDOS_API.TABLAS;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDbContext<SISTEM_CONTEXT>(
options =>
{
options.UseSqlServer("server = DESKTOP-RCQQR4T,1433; database = PARTIDOS; user = joacosa; password = 1234xd;TrustServerCertificate=true;");
});
builder.Services.AddTransient<IUsuarioRepository, UsuarioRepository>();
builder.Services.AddTransient<ICanchaRepository, CanchaRepository>();
builder.Services.AddTransient<ICuentaRepository, CuentaRepository>();
builder.Services.AddTransient<IDireccionRepository, DireccionRepository>();
builder.Services.AddTransient<ILocalidadRepository, LocalidadRepository>();
builder.Services.AddTransient<IPaisRepository, PaisRepository>();
builder.Services.AddTransient<IPerfilRepository, PerfilRepository>();
builder.Services.AddTransient<IPredioRepository, PredioRepository>();
builder.Services.AddTransient<IProvinciaRepository, ProvinciaRepository>();
builder.Services.AddTransient<ITurnoRepository, TurnoRepository>();
builder.Services.AddTransient<IVinculoRepository, VinculoRepository>();
builder.Services.AddTransient<IDiaRepository, DiaRepository>();
builder.Services.AddTransient<IHorariosDisponibles, HorariosDisponiblesRepository>();
builder.Services.AddTransient<IPartidaRepository, PartidaRepository>();
builder.Services.AddTransient<IPartidaUsuarioRepository, PartidaUsuarioRepository>();
builder.Services.AddTransient<IInvitacionRepository, InvitacionRepository>();
builder.Services.AddTransient<IDeporteRepository, DeporteRepository>();
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAnyOrigin",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseRouting();
// Habilitar CORS
app.UseCors("AllowAnyOrigin");
app.UseAuthorization();
app.MapControllers();
app.Run();