Skip to content

Pass DbContext to RelationalCommandParameterObject #36286

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

martincostello
Copy link
Member

Make DbContext available from RelationalCommandParameterObject for interception with SQLite and querying enumerables.

The functionality to add the context to the interception was added originally by #16162, but the context wasn't passed through for SQLite for some reason.

I found this by chance via some changes for OpenTelemetry in open-telemetry/opentelemetry-dotnet-contrib#2829 where an exception was being thrown internally due to an assumption that the context is always non-null here and here.

It's used in SqliteHistoryRepository here, but not in the other two locations for SQLite, which this PR changes.

While I was at it, I looked at all the other call sites for the constructor and changed any that had access to the DbContext but also just passed through null.

I haven't added any tests for this as it wasn't obvious to me where to add/edit existing tests for this. There wasn't any obvious SQLite-specific place to put tests based on reviewing the diff from #16162. I can add some if some guidance is provided on where they should go.

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

- Make `DbContext` available from `RelationalCommandParameterObject` for interception with SQLite and querying enumerables.

Relates to dotnet#16159
@Copilot Copilot AI review requested due to automatic review settings June 24, 2025 12:02
@martincostello martincostello requested a review from a team as a code owner June 24, 2025 12:02
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Updates to ensure the DbContext is passed into RelationalCommandParameterObject for SQLite commands and LINQ query execution, enabling interception scenarios (e.g., OpenTelemetry).

  • Pass Dependencies.CurrentContext.Context instead of null in SqliteDatabaseCreator.Create() and HasTables()
  • Pass _relationalQueryContext.Context instead of null in the CreateDbCommand methods of several relational query enumerables

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
src/EFCore.Sqlite.Core/Storage/Internal/SqliteDatabaseCreator.cs Pass the current DbContext to relational command factory instead of null
src/EFCore.Relational/Query/Internal/SingleQueryingEnumerable.cs Pass the query context’s DbContext into command creation
src/EFCore.Relational/Query/Internal/GroupBySplitQueryingEnumerable.cs Pass the query context’s DbContext into command creation
src/EFCore.Relational/Query/Internal/GroupBySingleQueryingEnumerable.cs Pass the query context’s DbContext into command creation
Comments suppressed due to low confidence (1)

src/EFCore.Sqlite.Core/Storage/Internal/SqliteDatabaseCreator.cs:55

  • Missing tests for this change: add unit or functional tests that verify the DbContext is correctly passed into RelationalCommandParameterObject for SQLite commands (e.g., in a SqliteHistoryRepository interception scenario).
                    Dependencies.CurrentContext.Context,

martincostello added a commit to martincostello/opentelemetry-dotnet-contrib that referenced this pull request Jun 24, 2025
Avoid `ArgumentException` if the `DbContext` is not available in the event payload.

See dotnet/efcore#36286.
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.

1 participant