Have a better story for EFCore.PG and multihost
Los mantenedores suelen responder en 1 día
@roji ya está trabajando en esto.
Desde el 10/3/2025.
Evaluación
Este issue todavía no se ha evaluado.
Descripción
Try to have Target Session Attributes in the connection string fails the moment EFCore.PG needs to create a data source internally (full repro below):
.UseNpgsql(
"Host=x,y;Database=test;Username=test;Password=test;Load Balance Hosts=true;Target Session Attributes=prefer-standby",
config =>
{
config.ConfigureDataSource(o => {});
})
This throws "When creating a multi-host data source, TargetSessionAttributes cannot be specified".
The Npgsql story for target sessions is to call BuildMultiHost() instead of Build(), and then to call an overload of OpenConnectionAsync() that passes TargetSessionAttributes, or to extract wrapper data sources via WithTargetSession() (docs).
EFCore.PG doesn't know whether the connection string contains multiple hosts and/or Target Session Attribute, and never calls BuildMultiHost(). It seems like the thing to do would be to add an EF-level context option for the target session, and when EF creates the NpgsqlConnection, to call OpenConnectionAsync(TargetSessionAttributes).
Note that we also have legacy mode (without NpgsqlDataSource), where we do allow Target Session Attribute in the connection string; we internally create a wrapper for the given target session and use that. We could do the same when NpgsqlDataSourceBuilder.Build() is called with a Target Session Attribute; but that would mean that the underlying NpgsqlMultiHostDataSource (which owns the actual connection pools) isn't accessible, and it's impossible to have multiple Target Session Attribute values referencing the same physical connections. In legacy mode the NpgsqlMultiHostDataSource is global, so this isn't a problem.
In the meantime, the workaround is simply to construct a multi-host data source outside EF, get a wrapper via WithTargetSession() and pass that to EFCore.PG's UseNpgsql().
@NinoFloris @vonzshik does this all make sense?
Originally raised by @fmendez89 in https://github.com/npgsql/doc/issues/263#issuecomment-2708845653
Full repro
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseNpgsql(
"Host=localhost,localhost:6432;Database=test;Username=test;Password=test;Load Balance Hosts=true;Target Session Attributes=prefer-standby",
config =>
{
// The moment we call ConfigureDataSource(), EF attempts to build a data source internally (with NpgsqlDataSourceBuilder.Build()),
// causing an exception because Target Session Attributes is present in the connection string.
config.ConfigureDataSource(o => {});
})
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}
- Lenguaje dominante
- C#
- Estrellas
- 1.8k
- Forks
- 268
- Merge medio
- 2 h 35 min
- PR fusionados (30 d)
- 136
Preparar el entorno
Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de npgsql/efcore.pg
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Los mantenedores suelen responder en 1 día
-
NTS: the M ordinate is dropped from geometry SQL literals and from geometries stored in JSONPosiblemente ocupada @Ahmed7991 la tomó hace 5 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 85/100
npgsql/efcore.pg#4093 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
`TimeSpan`/`Duration` component members: negative values are floored, and intervals computed in SQL are read field by fieldPosiblemente ocupada @jeremy-morren la tomó hace 11 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 1 día
-
Numeric storage parameters are formatted with the current culture, so migrations emit 0,01 instead of 0.01Posiblemente ocupada @Sorbitol la tomó hace 12 días. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 30/100
Los mantenedores suelen responder en 1 día
-
`Contains` on a JSON primitive collection compares JSON text, so `DateTime` and `TimeOnly` items never matchPosiblemente ocupada @Mr444-dev la tomó hace 7 días. Abierto
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
npgsql/efcore.pg#4099 · 1 comentario ·
Los mantenedores suelen responder en 1 día
Todos los issues de npgsql/efcore.pg
Issues similares
-
Issue-Bug Needs-Triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 2 días
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
JiaFeiMiao-K-Cat/DiscImageStudio#281 ·
Los mantenedores suelen responder en 1 día
-
area/access-management Frontend onboarding candidate :handshake:
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Altinn/altinn-auth#4941 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
aws/aws-sdk-net#4586 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
Kryptos-FR/MarkView.Avalonia#269 · 1 reacción ·
Los mantenedores suelen responder en 1 día