Comparing a complex type to an inline instance whose nested optional complex property is null drops all other comparisons
メンテナーはふだん 2 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 35/100
調査の方向性
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時間
- マージ済み PR(30日)
- 158
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
dotnet/efcore のほかの issue
-
Test bug: Include_collection_with_cross_apply_with_filter relies on nondeterministic ORDER BY with Take対応中かも @Mohamedadel-prog が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
dotnet/efcore#39848 · コメント 1 件 ·
メンテナーはふだん 2 日以内に返信
-
customer-reported
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 2 日以内に返信
-
Compiled model: generate a deterministic model ID対応中かも @rgroenewoudt が今日担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
メンテナーはふだん 2 日以内に返信
-
customer-reported
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
メンテナーはふだん 2 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
メンテナーはふだん 2 日以内に返信
似ている issue
-
area-VM-meta-mono untriaged
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
microsoft/sqltoolsservice#2851 ·
メンテナーはふだん 1 日以内に返信
-
enhancement
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
needs-triage
難易度 2/5 1〜3時間 初心者へのやさしさ 83/100
elsa-workflows/elsa-foundation#6322 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 74/100
mohebifar/made-in-iran#273 ·