Skip to content

Commit bb614e4

Browse files
github-actions[bot]Copilot
authored andcommitted
fix: correct SqlMetaData constructor selection for datetime2/datetimeoffset/time TVP columns
The previous fix for #393 used `match name, dbType with` where `name` is the column name (e.g. "CreatedAt"), not the SQL type name. This meant the pattern | "datetime2", SqlDbType.DateTime2 -> ... would never fire unless the column happened to be literally named "datetime2". The correct fix is to match on `dbType` alone, which always contains the actual SQL type. This ensures that all DateTime2, DateTimeOffset and Time TVP columns use SqlMetaData(name, dbType, 0uy, scale) rather than SqlMetaData(name, dbType, precision, scale), avoiding the ArgumentException "Metadata for field 'X' did not match the original record's metadata" when the precision byte doesn't equal what SQL Server expects. Closes #393 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ac79dd6 commit bb614e4

1 file changed

Lines changed: 8 additions & 4 deletions

File tree

src/SqlClient.DesignTime/DesignTime.fs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,15 @@ type DesignTime private() =
542542
let precision = byte p.Precision
543543
let scale = byte p.Scale
544544
if typeInfo.IsFixedLength then
545-
match name, dbType with
546-
| "datetime2" , SqlDbType.DateTime2
547-
| "datetimeoffset", SqlDbType.DateTimeOffset
548-
| "time" , SqlDbType.Time ->
545+
match dbType with
546+
| SqlDbType.DateTime2
547+
| SqlDbType.DateTimeOffset
548+
| SqlDbType.Time ->
549549
// https://github.com/fsprojects/FSharp.Data.SqlClient/issues/393
550+
// These types use (name, dbType, precision, scale) but precision must be 0,
551+
// not the value from sys.columns (which is 10 for datetimeoffset etc.).
552+
// The previous match on `name` was incorrect — it compared the column name
553+
// (e.g. "CreatedAt") to the type name, so the fix never fired.
550554
<@@ SqlMetaData(name, dbType, 0uy, scale) @@>
551555
| _ ->
552556
// https://github.com/fsprojects/FSharp.Data.SqlClient/issues/345

0 commit comments

Comments
 (0)