© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
1 reply
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;
        }
    }
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

KeyNotFoundException: The given key was not present in the dictionary.
C#CC# / help
3y ago
❔ System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary
C#CC# / help
3y ago
The Given key was not present in the dictionary
C#CC# / help
16mo ago
the given key "server id" was not present in the dictionary
C#CC# / help
2y ago