Skip to content

Conversation

Copy link

Copilot AI commented Jan 12, 2026

The NetTopologySuite SQL Server provider had a hard dependency on SqlDataReader.GetSqlBytes(), preventing custom DbDataReader implementations injected via interceptors from reading geometry data.

Changes

  • SqlServerGeometryTypeMapping.cs: Changed _getSqlBytes to use DbDataReader.GetFieldValue<SqlBytes>() instead of SqlDataReader.GetSqlBytes()
  • SqlServerGeometryTypeMappingTests.cs: Added test verifying the method works with DbDataReader base class

Technical Details

Before:

private static readonly MethodInfo _getSqlBytes
    = typeof(SqlDataReader).GetRuntimeMethod(nameof(SqlDataReader.GetSqlBytes), [typeof(int)])!;

After:

private static readonly MethodInfo _getSqlBytes
    = typeof(DbDataReader).GetRuntimeMethod(nameof(DbDataReader.GetFieldValue), [typeof(int)])!
        .MakeGenericMethod(typeof(SqlBytes));

This follows the pattern already used by SqliteGeometryTypeMapping and aligns with the base RelationalTypeMapping implementation. The change maintains full backwards compatibility since SqlDataReader inherits GetFieldValue<T>() from DbDataReader.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • kijvsblobprodcus387.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/efcore/efcore/.dotnet/dotnet dotnet build src/EFCore.SqlServer.NTS/EFCore.SqlServer.NTS.csproj --no-restore (dns block)
  • m8dvsblobprodcus37.vsblob.vsassets.io
    • Triggering command: /home/REDACTED/work/efcore/efcore/.dotnet/dotnet /home/REDACTED/work/efcore/efcore/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:false /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/efcore/efcore/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/efcore/efcore/artifacts/toolset/11.0.0-beta.25578.104.txt (dns block)
    • Triggering command: /home/REDACTED/work/efcore/efcore/.dotnet/dotnet dotnet build src/EFCore.SqlServer.NTS/EFCore.SqlServer.NTS.csproj --no-restore (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Problem

The Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite package currently has a hard dependency on SqlDataReader that prevents it from working with custom DbDataReader-derived classes injected via interceptors.

Current Issue

In src/EFCore.SqlServer.NTS/Storage/Internal/SqlServerGeometryTypeMapping.cs (lines 25-26 and 108-109):

private static readonly MethodInfo _getSqlBytes
    = typeof(SqlDataReader).GetRuntimeMethod(nameof(SqlDataReader.GetSqlBytes), [typeof(int)])!;

public override MethodInfo GetDataReaderMethod()
    => _getSqlBytes;

This code specifically references SqlDataReader.GetSqlBytes(), which means any custom DbDataReader implementation from interceptors will fail when trying to read geometry data because:

  • The method is looked up on SqlDataReader specifically (not DbDataReader)
  • Custom DbDataReader implementations won't have the GetSqlBytes method
  • Casting to SqlDataReader would fail for custom implementations

Required Changes

Modify the SqlServerGeometryTypeMapping<TGeometry> class in src/EFCore.SqlServer.NTS/Storage/Internal/SqlServerGeometryTypeMapping.cs to work with DbDataReader instead of SqlDataReader:

  1. Replace the SqlDataReader.GetSqlBytes method reference with DbDataReader.GetFieldValue<SqlBytes>
  2. Update the _getSqlBytes field to reference the generic GetFieldValue method from DbDataReader
  3. Ensure the GetDataReaderMethod() returns a method that works with any DbDataReader-derived class

The solution should use:

private static readonly MethodInfo _getSqlBytes
    = typeof(DbDataReader).GetRuntimeMethod(nameof(DbDataReader.GetFieldValue), [typeof(int)])!
        .MakeGenericMethod(typeof(SqlBytes));

This way, it will work with:

  • Standard SqlDataReader
  • Custom DbDataReader-derived classes from interceptors
  • Any implementation that properly implements GetFieldValue<T>

Code Review Guidelines

  • Check the IsDeleted flag when retrieving entities from the database
  • Suggest removing code chunks that have been commented out
  • Ignore changes to any mortar directories as this is auto-generated code

Goal

Enable the NetTopologySuite SQL Server provider to work seamlessly with custom DbDataReader implementations, allowing users to:

  1. Use interceptors that inject custom DbDataReader-derived classes
  2. Build this modified branch locally
  3. Reference the DLL directly in their projects instead of the NuGet package

Files to Modify

  • src/EFCore.SqlServer.NTS/Storage/Internal/SqlServerGeometryTypeMapping.cs
    • Line 25-26: Update the _getSqlBytes field initialization
    • Ensure line 108-109 continues to work correctly with the new method reference

This pull request was created from Copilot chat.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…stead of SqlDataReader.GetSqlBytes

Co-authored-by: kierenj <82343+kierenj@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor geometry type mapping to support custom DbDataReader Remove SqlDataReader dependency from SqlServerGeometryTypeMapping Jan 12, 2026
Copilot AI requested a review from kierenj January 12, 2026 11:26
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.

2 participants