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

Open
#39,826 0 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 2 days

@cincuranet is already working on this.

Since Nov 27, 2026.

  • #39829 by @seragAhmed — open

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
45/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
csharp, sqlite
Domain
database

Research direction

Start in src/EFCore.Sqlite.NTS/Query/Internal/SqliteGeometryMemberTranslator.cs and compare the Geometry.Length mapping with the provider behavior described here. Reproduce the square query using the supplied setup and check the SQLite spatial function used for polygon length. Done means the SQLite query returns the polygon perimeter, 16, consistently with NetTopologySuite; an open linked pull request indicates work is already underway.

Written by the indexing model from the issue text.

Description

area-query area-sqlite

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
Dominant language
C#
Stars
14.8k
Forks
3.5k
Avg merge
3d 7h
Merged PRs (30d)
182

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.