UseAzureSql at 170: Contains over a list inside a JSON collection applies JSON_CONTAINS to an nvarchar(max) fragment (error 8116)
Maintainer thường phản hồi trong vòng 2 ngày
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 48/100
- Loại issue
- Lỗi
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend-api-design, databases
Hướng nghiên cứu
Start by tracing the SQL Server provider translation for Contains over primitive collections nested in JSON collections, especially the OPENJSON and JSON_CONTAINS paths affected by Azure SQL compatibility level 170. Compare the generated SQL for UseAzureSql and UseSqlServer, then add or find a regression test for the supplied model and query. Done means the query works on both provider configurations without errors 8116 or 13618.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Bug description
With UseAzureSql at compatibility level 170, Contains over a primitive collection inside a JSON-mapped collection fails:
Microsoft.Data.SqlClient.SqlException (0x80131904): Argument data type nvarchar(max) is invalid for argument 1 of json_contains function.
This looks like an interaction between two changes:
- The Azure SQL fix for #38615 (PR #38665): with
UseAzureSql, the nested fragment inOPENJSON ... WITHis declarednvarchar(max) ... AS JSONinstead ofjson ... AS JSON. - The
JSON_CONTAINStranslation (#36656 / #37562): it is then applied to that fragment.JSON_CONTAINSonly accepts thejsontype as its first argument.
Generated SQL with UseAzureSql:
WHERE EXISTS (
SELECT 1
FROM OPENJSON([b].[Details], '$.Tags') WITH ([Names] nvarchar(max) '$.Names' AS JSON) AS [t]
WHERE JSON_CONTAINS([t].[Names], N'a') = 1)
With UseSqlServer the fragment is declared json and the query works on SQL Server 2025. On Azure SQL Database, though, that form is the one #38615 fixed (error 13618). So on Azure SQL at level 170 this query shape fails with either provider configuration.
Possible fixes: keep the OPENJSON translation for Contains over a fragment that was declared nvarchar(max), or wrap the fragment in CAST(... AS json) before JSON_CONTAINS. JSON_CONTAINS(CAST(JSON_QUERY(...) AS json), ...) works on both SQL Server 2025 and Azure SQL.
Your code
// dotnet run repro.cs -- "<connection string to an empty database on SQL Server 2025 or Azure SQL Database>"
#:property PublishAot=false
#:package [email protected]
using Microsoft.EntityFrameworkCore;
var options = new DbContextOptionsBuilder<BlogContext>()
.UseAzureSql(args[0], o => o.UseCompatibilityLevel(170))
.Options;
await using var context = new BlogContext(options);
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();
context.Blogs.Add(new Blog { Details = new Details { Tags = [new Tag { Names = ["a"] }] } });
await context.SaveChangesAsync();
// FAIL: Argument data type nvarchar(max) is invalid for argument 1 of json_contains function.
await context.Blogs.CountAsync(b => b.Details.Tags.Any(t => t.Names.Contains("a")));
public class Blog
{
public int Id { get; set; }
public Details Details { get; set; } = new();
}
public class Details
{
public List<Tag> Tags { get; set; } = [];
}
public class Tag
{
public List<string> Names { get; set; } = [];
}
public class BlogContext(DbContextOptions<BlogContext> options) : DbContext(options)
{
public DbSet<Blog> Blogs => Set<Blog>();
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Blog>().OwnsOne(b => b.Details, d =>
{
d.ToJson();
d.OwnsMany(x => x.Tags);
});
}
Stack traces
Microsoft.Data.SqlClient.SqlException (0x80131904): Argument data type nvarchar(max) is invalid for argument 1 of json_contains function.
Error Number:8116,State:1,Class:16
EF Core version
11.0.0
Database provider
Microsoft.EntityFrameworkCore.SqlServer 11.0.0 (UseAzureSql). Reproduced on SQL Server 2025 (17.0.4025.3); the same JSON_CONTAINS argument check applies on Azure SQL Database.
Target framework
.NET 11.0
Operating system
Windows 11
IDE
No response
- Ngôn ngữ chính
- C#
- Star
- 14.8k
- Fork
- 3.4k
- Merge trung bình
- 3 ngày 18 giờ
- Pull request đã merge (30 ngày)
- 159
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
-
customer-reported
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 54/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
-
Regression in 11.0: replacing an owned instance or re-adding an entity with the same key sets unchanged OnAdd properties to nullCó thể đã có người làm @seragAhmed đã 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 48/100
dotnet/efcore#39617 · 1 bình luận ·
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
-
customer-reported
Độ khó 5/5 Hơn một tuần 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
Tất cả issue của dotnet/efcore
Issue tương tự
-
bug emitter:client:csharp
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
microsoft/typespec#12572 · 1 bình luận · 1 reaction ·
Maintainer thường phản hồi trong vòng 1 ngày
-
needs-area-label
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
Maintainer thường phản hồi trong vòng 2 ngày
-
AWS Lambda: KeyNotFoundException drops HTTP-triggered transactions when allowAllHeaders is disabledĐang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
newrelic/newrelic-dotnet-agent#3949 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area-networking
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
dotnet/aspnetcore#70545 · 1 bình luận ·
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 87/100
Maintainer thường phản hồi trong vòng 1 ngày