Skip to content

SQLite: Polygon.Length translates to GLength, which returns 0 #39826

In review

Description

@roji

Description

With the SQLite NetTopologySuite provider, Polygon.Length is translated to GLength, which returns 0 for a polygon. NetTopologySuite returns the polygon's perimeter instead. For the 4×4 square below, the expected result is 16 but the query returns 0.

The SQLite provider's member translator maps Geometry.Length to GLength. The 11.0.0 translator uses the same mapping, although I have not run the repro against 11.0.0.

Related: npgsql/efcore.pg#4151 reports the same mismatch in the PostgreSQL provider's independent translation.

Reproduction

Create a .NET 10 console project with the following package references and install the native mod_spatialite library as described in the SQLite spatial documentation. The repro uses the system SQLite provider, as recommended there for macOS and Linux.

<ItemGroup>
  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.Core" Version="10.0.15" />
  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite.NetTopologySuite" Version="10.0.15" />
  <PackageReference Include="SQLitePCLRaw.provider.sqlite3" Version="2.1.10" />
</ItemGroup>
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using NetTopologySuite.Geometries;

SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
var factory = new GeometryFactory(new PrecisionModel(), 4326);
await using var context = new TestContext();
await context.Database.OpenConnectionAsync();
await context.Database.EnsureCreatedAsync();

var polygon = factory.CreatePolygon([new(0, 0), new(4, 0), new(4, 4), new(0, 4), new(0, 0)]);
context.Rows.Add(new Row { Polygon = polygon });
await context.SaveChangesAsync();

Console.WriteLine($"NTS: {polygon.Length}, SQLite: {await context.Rows.Select(r => r.Polygon.Length).SingleAsync()}");

public class TestContext : DbContext
{
    public DbSet<Row> Rows => Set<Row>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlite("Data Source=:memory:", o => o.UseNetTopologySuite())
            .LogTo(Console.WriteLine, LogLevel.Information);

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Row>().Property(r => r.Polygon).HasSrid(4326);
}

public class Row
{
    public int Id { get; set; }
    public Polygon Polygon { get; set; } = null!;
}

Output:

SELECT GLength("r"."Polygon")
FROM "Rows" AS "r"
LIMIT 2

NTS: 16, SQLite: 0

Environment

  • EF Core SQLite NetTopologySuite: 10.0.15
  • SpatiaLite: 5.1.0
  • .NET: 10.0.8
  • macOS arm64

Activity

  1. added theissue type on Nov 26, 2026
  2. self-assigned this
    on Nov 27, 2026
  3. added this to the 12.0.0 milestone on Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions