Skip to content

Comments

Fix NTS geometries stored as invalid geographies in SQL Server#37784

Draft
Copilot wants to merge 2 commits intomainfrom
copilot/fix-invalid-geographies-sql-server
Draft

Fix NTS geometries stored as invalid geographies in SQL Server#37784
Copilot wants to merge 2 commits intomainfrom
copilot/fix-invalid-geographies-sql-server

Conversation

Copy link
Contributor

Copilot AI commented Feb 24, 2026

SqlServerBytesWriter v2.1.0 sets the IsValid flag in the SQL Server binary format (MS-SSCLRT) based on NTS's Geometry.IsValid. NTS and SQL Server use different validation rules, so geometries NTS considers invalid (e.g., minor ring self-intersections common in real-world GIS data) get the V flag set to false. SQL Server's STIsValid() reads this flag from the binary without re-validating, returning 0.

  • GeometryValueConverter: Post-process the serialized bytes from SqlServerBytesWriter to always set the IsValid flag (bit 2 of the Properties byte at offset 5) to true, decoupling NTS validation from SQL Server's validity flag
// Before: NTS IsValid propagated to SQL Server V flag
g => new SqlBytes(writer.Write(g))

// After: V flag always set to true
g => new SqlBytes(SetIsValidFlag(writer.Write(g)))
  • Unit tests: Added tests covering valid/invalid geometries and geographies, verifying the flag is always set and roundtrip data is preserved
Original prompt

This section details on the original issue you should resolve

<issue_title>Some NTS Geometry objects endup as invalid Geographies in Sql Server</issue_title>
<issue_description>### Bug description

Context

  • Windows 11
  • SQL Server 2017
  • EF Core 8.0.18 (I also tried on .NET 10 and EF Core 10.0.1, and same issue)

Problem
I build a NTS Geometry object and assign it to an object's field, which is then persisted to a table using EF Core.

The record is stored in the database properly, but when I do:

SELECT Geography.STIsValid() FROM MyTable

It returns 0

The issue only happens with certain shapes (the attached one is an example)

geo.txt

Your code

class MyTable
        {
            public int Id { get; set; }

            public Geometry? Geography { get; set; }
        }

        class MyDBContext : DbContext
        {
            public DbSet<MyTable> MyTable { get; set; }

            public MyDBContext(DbContextOptions<MyDBContext> options) : base(options)
            {
            }
        }

        [Test]
        public void TestDodgyGeography()
        {
            var options = new DbContextOptionsBuilder<MyDBContext>()
                .UseSqlServer("Data Source=localhost;Initial Catalog=MyDB;User ID=sa;Password=Pass1234;Trust Server Certificate=true", x => x.UseNetTopologySuite())
                .Options;
            var context = new MyDBContext(options);
            var geo = new WKTReader(new NtsGeometryServices(PrecisionModel.Floating.Value, 4269)).Read("MY MULTIPOLYGON IS TOO BIG SO I HAVE ADDED IT AS AN ATTACHMENT");
            context.MyTable.Add(new MyTable { Id = 1, Geography = geo });
            context.SaveChanges();
        }

Stack traces


Verbose output


EF Core version

8.0.18

Database provider

Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 8

Operating system

Windows 11

IDE

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

…ary output

SqlServerBytesWriter v2.1.0 sets the IsValid flag in the SQL Server binary
format based on NTS's Geometry.IsValid, which uses different validation rules
than SQL Server. SQL Server's STIsValid() reads the V flag from the binary
format without re-validating. This causes geometries that are invalid in NTS
(but potentially valid in SQL Server) to be incorrectly marked as invalid.

The fix ensures the IsValid flag in the binary output is always set to true,
preventing NTS validation rules from affecting SQL Server validity.

Fixes #37416

Co-authored-by: AndriySvyryd <6539701+AndriySvyryd@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix invalid NTS Geometry objects in SQL Server Fix NTS geometries stored as invalid geographies in SQL Server Feb 24, 2026
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.

Some NTS Geometry objects endup as invalid Geographies in Sql Server

2 participants