Skip to content

Commit 59e4deb

Browse files
github-actions[bot]Copilot
authored andcommitted
fix: improve error message when Microsoft.SqlServer.Types assembly is missing
When a database uses geography, geometry, or hierarchyid columns and the user has not referenced the Microsoft.SqlServer.Types NuGet package, the previous error was a cryptic TypeLoadException that gave no hint about the root cause. This change catches the null result from Type.GetType (non-throwing overload) and emits a clear message pointing to the missing NuGet package when the type name contains 'Microsoft.SqlServer.Types'. Closes #264 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent ceb4837 commit 59e4deb

1 file changed

Lines changed: 14 additions & 1 deletion

File tree

src/SqlClient/Shared.fs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,20 @@ and [<DataContract;CLIMutable>] TypeInfo = {
146146
[<DataMember>] UdttName : string /// Name of the user-defined-table-type
147147
[<DataMember>] TableTypeColumns : Column array /// Columns of the user-defined-table-type
148148
} with
149-
member this.ClrType: Type = if isNull this.ClrTypeFullName then null else Type.GetType( this.ClrTypeFullName, throwOnError = true)
149+
member this.ClrType: Type =
150+
if isNull this.ClrTypeFullName then null
151+
else
152+
match Type.GetType(this.ClrTypeFullName, throwOnError = false) with
153+
| null ->
154+
if this.ClrTypeFullName.Contains("Microsoft.SqlServer.Types") then
155+
failwithf
156+
"Could not load type '%s'. Your database uses the SQL Server spatial/hierarchy type '%s', \
157+
which requires the 'Microsoft.SqlServer.Types' NuGet package. \
158+
Add it to your project with: dotnet add package Microsoft.SqlServer.Types"
159+
this.ClrTypeFullName this.TypeName
160+
else
161+
Type.GetType(this.ClrTypeFullName, throwOnError = true) // rethrow original error for other types
162+
| t -> t
150163
member this.TableType = this.SqlDbType = SqlDbType.Structured
151164
member this.IsValueType = not this.TableType && this.ClrType.IsValueType
152165
member this.IsUnitOfMeasure = this.TypeName.StartsWith("<") && this.TypeName.EndsWith(">")

0 commit comments

Comments
 (0)