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 IServiceProvider access when registering DbContext #7017

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

aaronpowell
Copy link
Contributor

@aaronpowell aaronpowell commented Jan 3, 2025

Description

Adding overloads that allow you to provide a callback with access to the IServiceProvider when configuring a dbcontext.

Fixes #7016

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?
Microsoft Reviewers: Open in CodeFlow

@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jan 3, 2025
Copy link
Member

@eerhardt eerhardt left a comment

Choose a reason for hiding this comment

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

We should do this for all EF providers, not just 2 of them.

Also, we should be adding tests as well.

cc @sebastienros

/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="NpgsqlEntityFrameworkCorePostgreSQLSettings.ConnectionString"/> is not provided.</exception>
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple public overloads with optional parameters", Justification = "Supporting additional callback arguments")]
public static void AddNpgsqlDbContext<[DynamicallyAccessedMembers(RequiredByEF)] TContext>(
Copy link
Member

Choose a reason for hiding this comment

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

This isn't going to work well because the 2 methods have the same signature, so if you call builder.AddNpgsqlDbContext("name"), the compiler won't know which one you mean.

To fix this, we will need to expand the existing method out into overloads without default parameters. Similar to #6929 and #4327.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@aaronpowell aaronpowell requested a review from eerhardt January 16, 2025 05:24
@aaronpowell
Copy link
Contributor Author

All EF providers are now covered in the PR.

I haven't added any new tests, so if you want test coverage expanded let me know what you're after

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates that the PR has been added by a community member
Projects
None yet
Development

Successfully merging this pull request may close these issues.

npgsql ef integration doesn't provide a callback that accepts an IServiceProvider
2 participants