I have a certain setup on postgresql that works just fine using the psql shell.However, when I try to connect thoughout my Web App, I encounter a small bug.
Hey, I have a simple Postgresql 14 database setup that I will describe below :
A Database b contains two schemas : public and aph.
The public schema doesn't contain any relation, but the aph schema contains a test table named hey, with a single column acting as a primary key (defined as id int not null primary key).
All of these objects are owned by the postgres user.
A user named aph_auth, which can't login, is allowed to connect to the database cited above.
A user name aph_test inherits from aph_auth, which makes him able to connect to the database.
The aph_test user has a usage right on the schema and a select right on the table.
The setup looks something like this :
CREATE DATABASE b OWNER postgres;--connect to the databaseCREATE SCHEMA aph OWNER postgres;CREATE TABLE aph.hey(id int not null primary key);CREATE USER aph_auth NOLOGIN;CREATE USER aph_test INHERIT IN ROLE aph_auth;ALTER USER aph_test SET search_path TO aph;REVOKE ALL ON DATABASE b FROM public;REVOKE ALL ON SCHEMA aph FROM public;REVOKE ALL ON TABLE aph.hey FROM public;GRANT CONNECT ON DATABASE b TO aph_auth;GRANT USAGE ON SCHEMA aph TO aph_test;GRANT SELECT ON TABME aph.hey TO aph_test;Note that using the shell, there are no password / right issues with my setup, everything works perefectly.
Using this setup, I want to conenct to the database and retreive the number of rows in my table using Blazor Web App (.net 8).
Here is what I did :
Program.cs :
builder.Services.AddDbContext<AphContext>(options => options.UseNpgsql(builder.Configuration["Database:Aph"]));AphContext.cs :
public AphContext(DbContextOptions<AphContext> options) : base(options){}public DbSet<Hey> Heys { get; set; }protected override void OnModelCreating(ModelBuilder modelBuilder){ modelBuilder.Entity<Hey>().ToTable("hey"); base.OnModelCreating(modelBuilder);}Hey.cs :
public class Hey{ [Key] [Required] [Column("id")] public int Id { get; set; }}What my secrets.json file looks like :
{"Database": {"Aph": "Host=host; Database=b; Username=aph_test; Password=password; searchpath=aph;" }}Finally, I execute a count in my razor page :
@inject AphContext _aphContext<p>@_aphContext.Projects.Count()</p>This unfortunately doesn't work when I launch the app, but it does as soon as I refresh the page.I tried to replace the aph_test logins by postgres logins in my connection string, and it work at app launch this time.
I can't figure out why I have to refresh the page in order for it to work with this setup. Can anyone help ?
PS : I only have to refresh once after startup, no matter how many times i navigate back and forth to my page.