Skip to content

Insert through UseClickHouse(DbConnection) and follow runtime connection changes - #70

Draft
alex-clickhouse wants to merge 2 commits into
fix/preserve-code-only-connection-settingsfrom
fix/connection-config-swaps-and-dbconnection-writes
Draft

alex-clickhouse wants to merge 2 commits into
fix/preserve-code-only-connection-settingsfrom
fix/connection-config-swaps-and-dbconnection-writes

Conversation

@alex-clickhouse

Copy link
Copy Markdown
Collaborator

Stacked on #51. The base branch is fix/preserve-code-only-connection-settings. Review #51 first. CI runs only on PRs that target main, so this PR gets CI after #51 merges and the base changes to main.

Problem

Two defects in ClickHouseRelationalConnection, which share the same state:

  1. UseClickHouse(DbConnection) cannot write. GetClickHouseClient() throws unless the context has a ClickHouseDataSource, so SaveChanges and BulkInsertAsync always fail on this path.
  2. A runtime connection change does not reach inserts or the master connection. _dataSource is set in the constructor and never cleared. After SetConnectionString(...) or SetDbConnection(...), queries go to the new target, but inserts and the master connection (EnsureCreated, EnsureDeleted, migrations) go to the original one. On the connection-string path, a SetConnectionString before the first query has no effect. Because of this, dotnet ef database update --connection "..." applies migrations to the context's own connection string.

Server-free tests reproduced each case on the old code before the fix. Three more cases came up during the work, and are fixed too:

  • ChangeDatabase on a connection from a data source sent inserts to the data source database.
  • ChangeDatabase followed by SetConnectionString split queries and inserts across databases.
  • SetConnectionString with the unchanged string dropped code-only settings, such as BearerToken, from inserts.

Solution

Rule: inserts and the master connection use the same target as the current connection.

  • Connection from the data source, with the data source database: inserts use the data source client, as before. This is the usual connection-string path, and it does not change.
  • Any other ClickHouseConnection: the driver keeps the connection's client internal, so ClickHouseRelationalConnection owns a client made from a copy of the connection settings, with Database = connection.Database (this includes ChangeDatabase). The client is made again when the settings object or the database changes. It is disposed on Dispose, on SetDbConnection and on a connection-string change, and kept across a pooled-context reset.
  • ConnectionString setter: gets the provider-owned data source for the new string from ClickHouseDataSourceManager.GetOrCreateDataSource (now public). This removes a data source from UseClickHouse(DbDataSource), as Npgsql does.
  • SetDbConnection with a connection: clears the data source, as Npgsql does.
  • A non-ClickHouse connection without a ClickHouseDataSource still throws, and the message now gives the connection type.

Decisions for the reviewer

  • SetDbConnection(null) keeps the configured data source, so the next connection comes from it again.
  • An invalid connection string now throws in SetConnectionString, not at first use.
  • BulkInsertAsync now creates the connection object, so connection interceptors see it.
  • After SetConnectionString on a connection that already exists, inserts use one more client (and HTTP pool), because EF Core writes the new string into the existing connection.
  • Each distinct string given to SetConnectionString gets a cached data source in the singleton ClickHouseDataSourceManager, as each distinct string in UseClickHouse(string) does already. An application that switches between many tenant strings keeps one data source for each. Npgsql uses a plain connection after SetConnectionString instead.

Not changed: named connection strings (Name=...) are not resolved in the constructor, and the EnsureCreated database name ignores ChangeDatabase.

Tests

  • Ran: ConnectionTargetTests, 24 tests that need no server. They failed on the old code and pass now.
  • Compiled only: ConnectionWriteTargetTests (SaveChanges and BulkInsertAsync through a ClickHouseConnection, each swap case, ChangeDatabase, EnsureCreated after a swap, master connection target) and DotnetEfCliTests.Database_update_uses_connection_option. Docker was not available, so these integration tests did not run. CI will run them after the base changes to main.
  • Build: 0 errors, 349 warnings. The 8 new warnings are EF1001 (internal API) in the new tests, the same as in ConnectionSettingsTests.

Docs

Entries in the v0.3.1 "Bug fixes" sections of CHANGELOG.md and RELEASENOTES.md, and a paragraph in README.md on how inserts choose their client and target.

🤖 Generated with Claude Code

alex-clickhouse and others added 2 commits October 14, 2026 17:58
GetClickHouseClient() threw unless the context had a ClickHouseDataSource,
so SaveChanges and BulkInsertAsync failed with UseClickHouse(DbConnection).
For a ClickHouseConnection, the relational connection now creates and keeps
a ClickHouseClient from a copy of the connection settings, with the
database of the connection, and disposes it with the context.

The data source from the options was never cleared, so after
SetConnectionString or SetDbConnection, inserts and the master connection
used the original target, and a SetConnectionString before the first query
(as dotnet ef --connection does) had no effect. The ConnectionString setter
now gets the provider-owned data source for the new string, and
SetDbConnection with a connection clears the data source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex review found two cases where inserts used _dataSource although the
live connection had another target: ChangeDatabase followed by
SetConnectionString (the driver keeps the selected database), and
SetConnectionString with the string the connection already has (EF does
not change the connection, so it keeps its code-only settings). A
ChangeDatabase on a connection from a data source had the same problem.

The data source client and settings are now used only for the connection
that CreateDbConnection got from the data source, while it has the data
source database. Other ClickHouseConnections get the copied-settings
client. A connection of another type keeps the data source client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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