C#C
C#2y 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)


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);
});


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;
        }
    }
image.png
Was this page helpful?