Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support DbDataSource #2429

Merged
merged 1 commit into from
Oct 7, 2022
Merged

Support DbDataSource #2429

merged 1 commit into from
Oct 7, 2022

Conversation

roji
Copy link
Member

@roji roji commented Jul 13, 2022

This provides the basic support, allowing:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    => optionsBuilder.UseNpgsql(/* some data source instance */)

... or with DI:

var appServiceProvider = new ServiceCollection()
    .AddNpgsqlDataSource("Host=localhost;Username=test;Password=test")
    .AddDbContext<BlogContext>(o => o
        .UseNpgsql()
        .LogTo(Console.WriteLine, LogLevel.Information)
        .EnableSensitiveDataLogging())
    .BuildServiceProvider();

Notably excluded are:

Part of #2400

/cc @ajcvickers @NinoFloris @vonzshik

// Otherwise check the application service provider to see if one is registered there.
static DbDataSource? GetDataSource(IDbContextOptions options, NpgsqlOptionsExtension npgsqlOptions)
=> npgsqlOptions.DataSource
?? options.FindExtension<CoreOptionsExtension>()?.ApplicationServiceProvider?.GetService<DbDataSource>();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Safer to do the following

Suggested change
?? options.FindExtension<CoreOptionsExtension>()?.ApplicationServiceProvider?.GetService<DbDataSource>();
?? options.FindExtension<CoreOptionsExtension>()?.ApplicationServiceProvider?.GetService<NpgsqlDataSource>();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thing is that we want to support a wrapping data source as well, which wouldn't be an NpgsqlDataSource.

Basically I think that if a DbDataSource is registered in DI, and the user hasn't specified a connection string or connection in UseNpgsql, it makes sense to pick that up. What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to leave the wrapping DbDataSource scenario to manual config. The reason I gave the suggestion is that you don't want to blow up down the line because DbDataSource is actually of type SqliteDataSource and it doesn't quite work the same.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But wouldn't that happen only if you don't otherwise explicitly configure EF (i.e. with a connection string), meaning that it will blow up anyway?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, we'll start by only resolving NpgsqlDataSource from DI, to minimize confusion in case an incompatible data source is present. Users who want wrappers can just use the explicit option with UseNpgsql.

@roji roji marked this pull request as ready for review September 17, 2022 17:41
@roji roji enabled auto-merge (rebase) September 17, 2022 17:43
@roji roji merged commit 5f289d5 into npgsql:main Oct 7, 2022
@roji roji deleted the DbDataSource branch October 7, 2022 10:50
@roji roji linked an issue Oct 7, 2022 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Basic support for NpgsqlDataSource
2 participants