SQL Server: a TimeSpan of a day or more in a JSON column is read back wrong, with no error, when a query reads the property
Maintainer thường phản hồi trong vòng 2 ngày
@AndriySvyryd đang làm issue này rồi.
Từ ngày 30/10/2026.
Đánh giá
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức phù hợp với người mới
- 57/100
Hướng nghiên cứu
Start by tracing the SQL Server translation of JSON-backed TimeSpan property access; the report shows the generated expression CAST(JSON_VALUE([h].[Doc], '$.Length') AS time). Check the provider tests for JSON projections and comparisons of TimeSpan, then add coverage for values of a day or more. Done means querying JSON-backed values gives the expected result or fails clearly, rather than returning a truncated time.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug description
A TimeSpan property inside a JSON column (OwnsOne(...).ToJson() or ComplexProperty(...).ToJson()) holding a day or more is stored correctly and reads back correctly when the entity is materialized. But a query that reads the property translates it to CAST(JSON_VALUE(...) AS time), and that returns a different value, with no error.
EF writes 26 hours into the JSON as "1:2:00:00". Converting that text to time gives 01:02:00, so the following are all wrong:
- a projection of the property returns
01:02:00; - a filter compares
01:02:00, so the row counts as "shorter than 2 hours".
SQL Server's time holds less than a day, so EF can write a value into JSON that it can't query back. Outside JSON the same value fails loudly: SaveChanges into a time column throws, and a parameter throws OverflowException. Here nothing does, and the answer depends on whether the property is materialized or queried.
I'd expect the query to answer 1.02:00:00, or to fail on translation or execution, rather than return a different time of day.
This is related to #39222, fixed by #39364 for 12.0, which makes an out-of-range TimeSpan literal throw. That fix doesn't reach this case: the value comes from the JSON document, not from a literal, so the CAST(... AS time) still runs and still answers without an error.
Your code
using Microsoft.EntityFrameworkCore;
await using (var setup = new Db())
{
await setup.Database.EnsureDeletedAsync();
await setup.Database.EnsureCreatedAsync();
setup.Add(new Holder { Doc = new() { Length = TimeSpan.FromHours(26) } });
await setup.SaveChangesAsync();
}
await using var db = new Db();
Console.WriteLine((await db.Holders.SingleAsync()).Doc.Length); // 1.02:00:00
Console.WriteLine(await db.Holders.Select(h => h.Doc.Length).SingleAsync()); // 01:02:00
Console.WriteLine(await db.Holders.CountAsync(h => h.Doc.Length < TimeSpan.FromHours(2))); // 1
Console.WriteLine(db.Holders.Select(h => h.Doc.Length).ToQueryString());
// SELECT CAST(JSON_VALUE([h].[Doc], '$.Length') AS time) FROM [Holders] AS [h]
public sealed class Holder
{
public int Id { get; set; }
public Doc Doc { get; set; } = new();
}
public sealed class Doc
{
public TimeSpan Length { get; set; }
}
public sealed class Db : DbContext
{
public DbSet<Holder> Holders => Set<Holder>();
protected override void OnConfiguring(DbContextOptionsBuilder options) =>
options.UseSqlServer("Server=localhost,1433;Database=TimeSpanJson;User Id=sa;Password=...;TrustServerCertificate=True");
protected override void OnModelCreating(ModelBuilder model) =>
model.Entity<Holder>().OwnsOne(h => h.Doc, doc => doc.ToJson()); // ComplexProperty(h => h.Doc, doc => doc.ToJson()) answers the same
}
The JSON column holds {"Length":"1:2:00:00"}.
Stack traces
None: no exception is thrown.
Verbose output
(not applicable)
EF Core version
10.0.14 (also seen on 10.0.12)
Database provider
Microsoft.EntityFrameworkCore.SqlServer 10.0.14, on Azure SQL Edge 15.0.2000.1574 (arm64). With 10.0.12, the same on SQL Server 2022 (Linux container).
Target framework
.NET 10.0
Operating system
macOS 27 (arm64), and Ubuntu (SQL Server 2022 run)
IDE
(none; dotnet CLI)
- Ngôn ngữ chính
- C#
- Star
- 14.8k
- Fork
- 3.5k
- Merge trung bình
- 2 ngày 22 giờ
- Pull request đã merge (30 ngày)
- 158
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/efcore
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with TakeCó thể đã có người làm @Mohamedadel-prog đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 66/100
dotnet/efcore#39848 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
-
customer-reported
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Compiled model: generate a deterministic model IDCó thể đã có người làm @rgroenewoudt đã nhận hôm nay. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Maintainer thường phản hồi trong vòng 2 ngày
-
customer-reported
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Maintainer thường phản hồi trong vòng 2 ngày
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của dotnet/efcore
Issue tương tự
-
area-VM-meta-mono untriaged
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 82/100
microsoft/sqltoolsservice#2851 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-triage
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 83/100
elsa-workflows/elsa-foundation#6322 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 74/100
mohebifar/made-in-iran#273 ·