C
C#10mo ago
Nefario

Blazor Server App. Error KeyNotFoundException: The given key was not present in the dictionary.

Hello i tried to connect my Blazor Server App to my postgresSQL, i already installed PostgresSQL and PgAdmin on my Docker and i've successfully connect my Pgadmin4 to postgressql, after setting up my server to connect i got this error.
KeyNotFoundException: The given key was not present in the dictionary.
Npgsql.NpgsqlConnectionStringBuilder.GeneratedSetter(string keyword, object value)

ArgumentException: Couldn't set user (Parameter 'user')
Npgsql.NpgsqlConnectionStringBuilder.set_Item(string keyword, object value)
KeyNotFoundException: The given key was not present in the dictionary.
Npgsql.NpgsqlConnectionStringBuilder.GeneratedSetter(string keyword, object value)

ArgumentException: Couldn't set user (Parameter 'user')
Npgsql.NpgsqlConnectionStringBuilder.set_Item(string keyword, object value)
Now here's my setting on program.cs
builder.Services.AddControllers();
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddMediatR(typeof(IAssemblyMarker).GetTypeInfo().Assembly);
builder.Services.AddTransient<IDbContext, DatabaseConnection>();
builder.Services.AddTransient<IUserService, UserService>();
builder.Services.AddTransient<IUserRepository, UserRepository>();

builder.Services.AddDbContext<DatabaseConnection>(options =>
{
var database = builder.Configuration.GetConnectionString("PostgrestSql");
options.UseNpgsql(database);
});
builder.Services.AddControllers();
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
builder.Services.AddMediatR(typeof(IAssemblyMarker).GetTypeInfo().Assembly);
builder.Services.AddTransient<IDbContext, DatabaseConnection>();
builder.Services.AddTransient<IUserService, UserService>();
builder.Services.AddTransient<IUserRepository, UserRepository>();

builder.Services.AddDbContext<DatabaseConnection>(options =>
{
var database = builder.Configuration.GetConnectionString("PostgrestSql");
options.UseNpgsql(database);
});
and here's how i tried to make connection to my to my database
public class UserRepository : IUserRepository
{
private IDbContext dbContext;

public UserRepository(IDbContext dbContext)
{
this.dbContext = dbContext;
}

public async Task<UserIdLogin> GetUser()
{
DbConnection dbConnection = dbContext.Connection;
var query = @"SELECT t.id FROM public.""Users"" t ";
var result = await dbConnection.QueryFirstOrDefaultAsync<UserIdLogin>(query);
return result;
}
}
public class UserRepository : IUserRepository
{
private IDbContext dbContext;

public UserRepository(IDbContext dbContext)
{
this.dbContext = dbContext;
}

public async Task<UserIdLogin> GetUser()
{
DbConnection dbConnection = dbContext.Connection;
var query = @"SELECT t.id FROM public.""Users"" t ";
var result = await dbConnection.QueryFirstOrDefaultAsync<UserIdLogin>(query);
return result;
}
}
No description
1 Reply
Nefario
NefarioOP10mo ago
and as for my appsetting.json
"ConnectionStrings": {
"PostgrestSql": "Server=localhost;Port=5432;Database=postgres;user=postgres;Pwd=local;"
},
"ConnectionStrings": {
"PostgrestSql": "Server=localhost;Port=5432;Database=postgres;user=postgres;Pwd=local;"
},
i dont see anything wrong with it, did i miss something? my postgres database running on port 5432 and as for my pgadmin running on 5050
Want results from more Discord servers?
Add your server