Skip to content

ConfigureDataSource ignored when UseNpgsql without ConnectionString parameter used #3692

Closed

Description

@nesc58

Hi,
I have tried to use Npgsql 10 with EF 10.

var optionsBuilder = new DbContextOptionsBuilder<MyPostgresContext>();
optionsBuilder.UseNpgsql(string.Empty, o =>
{
    o.ConfigureDataSource(ds => {
       d.ConnectionStringBuilder.Database = "MyDb";
       d.ConnectionStringBuilder.Port = 38521;
       d.ConnectionStringBuilder.Host= "127.0.0.1;
    });
    o.UseQuerySplittingBehavior(QuerySplittingBehavior.SingleQuery);
});
optionsBuilder.EnableSensitiveDataLogging();
optionsBuilder.EnableDetailedErrors();
optionsBuilder.UseQueryTrackingBehavior(QueryTrackingBehavior.TrackAll);
return new MyPostgresContext(optionsBuilder);

I have debugged a lot and found the error in the following file:

{ ConnectionString: null } when applicationServiceProvider?.GetService<NpgsqlDataSource>() is DbDataSource dataSource
=> dataSource,
// Otherwise if there's no connection string, abort: a connection string is required to create a data source in any case.
{ ConnectionString: null } or null => null,
// The following are features which require an NpgsqlDataSource, since they require configuration on NpgsqlDataSourceBuilder.
{ DataSourceBuilderAction: not null } => GetSingletonDataSource(npgsqlOptionsExtension),

When the connection string is not set the DataSourceBuilderAction is never called.

What I have missed to configure?

My current workaround is to set an empty connection string.

Activity

  1. added theissue type on Dec 23, 2025
  2. added a commit that references this issue on Dec 23, 2025
    72c5ca3
  3. roji commented on Dec 23, 2025

    @roji
    Member

    @nesc58 thanks, you seem to be right. I'll push a fix.

    Minimal runnable 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(
                    // Uncommenting the following makes the program work
                    // string.Empty,
                    o => o.ConfigureDataSource(b =>
                    {
                        b.ConnectionStringBuilder.Host = "localhost";
                        b.ConnectionStringBuilder.Username = "test";
                        b.ConnectionStringBuilder.Password = "test";
                    }))
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
  4. self-assigned this
    on Dec 23, 2025
  5. added a commit that references this issue on Dec 24, 2025
    1348621
  6. added a commit that references this issue on Dec 24, 2025
    4f9503f
  7. added this to the milestone on Dec 24, 2025
  8. added a commit that references this issue on Dec 24, 2025
    a64c405
  9. added a commit that references this issue on Apr 13, 2026
    caeb33b
  10. modified the milestones: , 10.0.4 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions