Skip to content

SQLite decimal collation fails under non-invariant cultures (e.g. tr-TR) #37432

Closed

Description

@salihozkara

Bug description

SQLite decimal collation in EF Core fails when the application is running under a culture that uses a decimal separator different from . (dot), such as tr-TR which uses , (comma).

The current implementation parses decimal values using the application's current culture, which causes incorrect ordering and comparison of decimal values in SQLite when the culture is not invariant.

This results in incorrect sorting behavior for decimal columns depending on the active CultureInfo.

Your code

[ConditionalFact, UseCulture("tr-TR")]
    public virtual void Can_query_OrderBy_decimal_with_Turkish_culture()
    {
        using var context = CreateContext();
        var min = new BuiltInDataTypes
        {
            Id = 225,
            PartitionId = 209,
            TestDecimal = 1.05m,
            TestDateTimeOffset = new DateTimeOffset(2018, 1, 1, 12, 0, 0, TimeSpan.Zero),
            TestTimeSpan = TimeSpan.FromDays(1),
            TestUnsignedInt64 = 0
        };
        context.Add(min);

        var middle = new BuiltInDataTypes
        {
            Id = 226,
            PartitionId = 209,
            TestDecimal = 1.5m,
            TestDateTimeOffset = new DateTimeOffset(2018, 1, 1, 12, 0, 0, TimeSpan.Zero),
            TestTimeSpan = TimeSpan.FromDays(2),
            TestUnsignedInt64 = 1
        };
        context.Add(middle);

        var max = new BuiltInDataTypes
        {
            Id = 227,
            PartitionId = 209,
            TestDecimal = 2.5m,
            TestDateTimeOffset = new DateTimeOffset(2018, 1, 1, 11, 0, 0, TimeSpan.FromHours(-2)),
            TestTimeSpan = TimeSpan.FromDays(10),
            TestUnsignedInt64 = long.MaxValue + 1ul
        };
        context.Add(max);

        context.SaveChanges();

        Fixture.TestSqlLoggerFactory.Clear();

        var query = context.Set<BuiltInDataTypes>()
            .Where(e => e.PartitionId == 209);

        var results = query
            .OrderBy(e => e.TestDecimal)
            .Select(e => new { e.Id, e.TestDecimal })
            .ToList();

        AssertSql(
            """
SELECT "b"."Id", "b"."TestDecimal"
FROM "BuiltInDataTypes" AS "b"
WHERE "b"."PartitionId" = 209
ORDER BY "b"."TestDecimal" COLLATE EF_DECIMAL
""");

        var expectedResults = query.AsEnumerable()
            .OrderBy(e => e.TestDecimal)
            .Select(e => new { e.Id, e.TestDecimal })
            .ToList();

        Assert.Equal(expectedResults.Count, results.Count);
        for (var i = 0; i < expectedResults.Count; i++)
        {
            Assert.Equal(expectedResults[i].Id, results[i].Id);
            Assert.Equal(expectedResults[i].TestDecimal, results[i].TestDecimal);
        }
    }

Stack traces


Verbose output


EF Core version

10.0.0

Database provider

Microsoft.EntityFrameworkCore.Sqlite

Target framework

.NET 10.0

Operating system

Windows 11

IDE

Rider

Activity

  1. roji commented on Jan 1, 2026

    @roji
    Member

    Confirmed bug, not a regression.

    AI Analysis

    The issue is that the EF_DECIMAL collation in the SQLite provider uses the current culture to parse decimal strings stored in the database. When the application runs under a culture like tr-TR (where the decimal separator is , and the group separator is .), it incorrectly parses the invariant-formatted decimal strings (e.g., 1.05) stored by EF Core. In tr-TR, 1.05 is parsed as 105, leading to incorrect ordering.

    Minimal repro
    CultureInfo.CurrentCulture = new CultureInfo("tr-TR");
    
    using var context = new MyContext();
    context.Database.EnsureDeleted();
    context.Database.EnsureCreated();
    
    context.Entities.AddRange(
        new MyEntity { Id = 1, Value = 1.05m },
        new MyEntity { Id = 2, Value = 1.5m },
        new MyEntity { Id = 3, Value = 2.5m }
    );
    context.SaveChanges();
    
    var results = context.Entities
        .OrderBy(e => e.Value)
        .Select(e => new { e.Id, e.Value })
        .ToList();
    
    foreach (var result in results)
    {
        Console.WriteLine($"Id: {result.Id}, Value: {result.Value}");
    }
    
    var expectedIds = new[] { 1, 2, 3 };
    if (!expectedIds.SequenceEqual(results.Select(r => r.Id)))
    {
        throw new Exception("Ordering failed!");
    }
    
    public class MyContext : DbContext
    {
        public DbSet<MyEntity> Entities { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlite("Data Source=repro.db")
                .LogTo(Console.WriteLine, LogLevel.Information);
    }
    
    public class MyEntity
    {
        public int Id { get; set; }
        public decimal Value { get; set; }
    }
  2. added theissue type on Jan 1, 2026
  3. added this to the 10.0.0 milestone on Jan 1, 2026
  4. self-assigned this
    on Jan 1, 2026
  5. added a commit that references this issue on Jan 3, 2026
    0792cd5
  6. modified the milestones: 10.0.0, 11.0.0 on Jan 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions