Comparing a complex type to an inline instance whose nested optional complex property is null drops all other comparisons

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

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
35/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
csharp, sqlite
领域
backend, databases

调研方向

Read RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs, especially TryGenerateComparisons around the lines cited in the issue, and trace how comparisons are accumulated through nested complex properties. Reproduce the query with SQLite and inspect its generated SQL; the issue reports the same failure with SQL Server. Done means inline instances with a null nested optional property preserve comparisons for other properties, including for !=.

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

描述

Bug description

When a table-split complex type is compared to an inline-constructed instance, and a nested optional complex property of that instance is null (or not set), EF generates only <nested required column> IS NULL and drops the comparisons for all other properties. The query silently returns rows that don't match. Using a captured variable (parameter) instead of an inline new generates the correct SQL. != is affected the same way.

The cause seems to be the "required property compared to a constant null" shortcut in TryGenerateComparisons (RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs, around L499-L508 on main): it assigns comparisons = comparison (overwriting the comparisons already accumulated for the outer complex type, since comparisons is passed by ref) and sets exitImmediately, which also stops all outer levels.

Your code
using Microsoft.EntityFrameworkCore;

using var ctx = new BlogContext();
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
ctx.Customers.AddRange(
    new Customer { Id = 1, Address = new() { City = "Seattle", Location = new() { Lat = 47.6 } } },
    new Customer { Id = 2, Address = new() { City = "Portland", Location = null } });
ctx.SaveChanges();

// Should match nothing: no customer has City == "Seattle" AND Location == null
var query = ctx.Customers.Where(c => c.Address == new Address { City = "Seattle", Location = null });

Console.WriteLine(query.ToQueryString());
Console.WriteLine("Matched ids: " + string.Join(",", query.Select(c => c.Id).ToList()));

public class Customer
{
    public int Id { get; set; }
    public Address Address { get; set; } = new();
}

public class Address
{
    public string City { get; set; } = "";
    public Location? Location { get; set; }
}

public class Location
{
    public double Lat { get; set; }
}

public class BlogContext : DbContext
{
    public DbSet<Customer> Customers => Set<Customer>();

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseSqlite("Data Source=repro.db");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
        => modelBuilder.Entity<Customer>(b =>
        {
            b.Property(c => c.Id).ValueGeneratedNever();
            b.ComplexProperty(c => c.Address, a => a.ComplexProperty(x => x.Location));
        });
}

Output:

SELECT "c"."Id", "c"."Address_City", "c"."Address_Location_Lat"
FROM "Customers" AS "c"
WHERE "c"."Address_Location_Lat" IS NULL
Matched ids: 2

Expected WHERE "c"."Address_City" = 'Seattle' AND "c"."Address_Location_Lat" IS NULL and no rows. SQL Server generates the same incomplete WHERE [c].[Address_Location_Lat] IS NULL. With a parameter (var a = new Address { City = "Seattle" }; ...Where(c => c.Address == a)) the SQL is correct, as it is for inline instances whose nested property is non-null.

Stack traces

No exception; incorrect results.

Verbose output

Not applicable; the generated SQL is shown above.

EF Core version

11.0.0 (also reproduced on 10.0.15 and on daily 12.0.0-alpha.1.26577.105)

Database provider

Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.SqlServer

Target framework

.NET 11.0

Operating system

Windows 10

IDE

dotnet CLI

I'd like to work on this and will wait for a maintainer to confirm before opening a PR.

主要语言
C#
星标
14.8k
派生
3.5k
平均合并
2 天 22 小时
30 天内合并 PR
158

环境准备

在 Codespaces 中打开

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

从这里开始

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

dotnet/efcore 的其他 Issue

查看 dotnet/efcore 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

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