UseAzureSql at 170: Contains over a list inside a JSON collection applies JSON_CONTAINS to an nvarchar(max) fragment (error 8116)

未关闭
#39,626 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
azure, csharp, json, postgresql, sql

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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 in OPENJSON ... WITH is declared nvarchar(max) ... AS JSON instead of json ... AS JSON.
  • The JSON_CONTAINS translation (#36656 / #37562): it is then applied to that fragment. JSON_CONTAINS only accepts the json type 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

主要语言
C#
星标
14.8k
派生
3.4k
平均合并
3 天 18 小时
30 天内合并 PR
159

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

dotnet/efcore 的其他 Issue

查看 dotnet/efcore 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。