Skip to content

[Repo Assist] fix: make connection parameter optional when literal connection string is configured (Closes #245)#452

Draft
github-actions[bot] wants to merge 2 commits intomasterfrom
repo-assist/fix-issue-245-optional-connection-dc2f1f1c5ae4f4a5
Draft

[Repo Assist] fix: make connection parameter optional when literal connection string is configured (Closes #245)#452
github-actions[bot] wants to merge 2 commits intomasterfrom
repo-assist/fix-issue-245-optional-connection-dc2f1f1c5ae4f4a5

Conversation

@github-actions
Copy link

@github-actions github-actions bot commented Mar 9, 2026

🤖 This is an automated PR from Repo Assist.

Summary

When SqlCommandProvider or SqlProgrammabilityProvider is configured with a literal connection string at design time (e.g. SqlProgrammabilityProvider<"Data Source=...">) the generated connection parameter in the runtime constructor/factory was incorrectly required, forcing users to first open a SqlConnection object even when a connection string was readily available.

Root Cause

In GetCommandCtors (DesignTime.fs), the connection parameter in parameters2 used:

?optionalValue = if designTimeConnectionString.IsDefinedByLiteral then None else Some null

When IsDefinedByLiteral = true, None makes the parameter required. However the runtime dispatch in body2 already handles null by falling back to connectionStringExpr (the literal string or config lookup), so making the parameter required was unnecessarily strict.

Fix

One-line change: always use ?optionalValue = Some null, making connection optional in all cases. The existing body2 logic handles all three cases:

  1. transaction provided → uses the transaction
  2. connection provided (non-null) → uses the connection
  3. neither provided (both null) → falls back to the design-time connection string

Effect

Users with a literal connection string can now write:

type AdventureWorks = SqlProgrammabilityProvider<"Data Source=...;...">

// Before (required an open connection even with a literal string):
use conn = new SqlConnection(connStr)
conn.Open()
let result = AdventureWorks.CreateCommand<"SELECT ...">(conn).Execute()

// After (connection is optional — literal string is reused at runtime):
let result = AdventureWorks.CreateCommand<"SELECT ...">().Execute()

// Explicit connection still works as before:
let result = AdventureWorks.CreateCommand<"SELECT ...">(myOpenConn).Execute()

The same improvement applies to SqlCommandProvider<"...">.Create(...) factory methods.

Test Status

Build: ✅ dotnet build src/SqlClient/SqlClient.fsproj -f netstandard2.0 succeeds (only pre-existing deprecation warnings).

The SqlClient.DesignTime project could not be built in this environment due to a pre-existing infrastructure issue (old Mono-based .paket/paket.bootstrapper.exe not available on Linux CI). The change is a one-line removal of a conditional expression; both branches already had type option(obj) and Some null is the value that was already used for the non-literal case. No logic in body2 changed.

Full integration tests require a SQL Server instance and could not be run in this environment.

Closes #245

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

…gured

When SqlCommandProvider or SqlProgrammabilityProvider is configured with
a literal connection string at design time, the connection parameter in
the generated constructor/factory method was incorrectly required. This
meant users had to pass an open SqlConnection even when a literal
connection string was available.

The fix makes the connection parameter always optional (defaulting to
null). The existing body2 logic already handles null by falling back to
the design-time connection string expression, so no further changes
are needed.

Users can now call:
  AdventureWorks.CreateCommand<"SELECT ...">().Execute()
instead of having to first open a SqlConnection.

Closes #245

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SqlProgrammabilityProvider requires open SqlConnection

0 participants