dotnet / dotnet/efcore

Entities with JSON owned entities can't be queried out with FromSql

オープン
#36,282 コメント 2 件 リアクション 1 件 担当者 0 名 GitHub で見る
area-json area-query
主要言語
C#
スター
14.8k
フォーク
3.4k
PR マージ指標
PR 指標を取得中

説明

For an entity type that has JSON owned entities, the following fails:

```c#
var items = await context.Blogs.FromSql($"WITH query AS (SELECT * FROM Blogs) SELECT * FROM query").ToListAsync();
```

It seems like when applying the final projection, we compose an additional SELECT on top:

```sql
SELECT b.Id, b.Name, b.Owned
FROM WITH query AS (SELECT * FROM Blogs) SELECT * FROM query
```

... and this (correctly) causes a non-composable SQL failure. We should not need to compose this extra SELECT on top.

With the focus on complex types for mapping JSON in EF 10, owned entities are getting de-prioritized. Given that, and given that we haven't seen many reports, i'll put this on the backlog for now.

Repro

```c#
await using var context = new BlogContext();
await context.Database.EnsureDeletedAsync();
await context.Database.EnsureCreatedAsync();

var items = await context.Blogs.FromSql($"WITH query AS (SELECT * FROM Blogs) SELECT * FROM query").ToListAsync();

public class BlogContext : DbContext
{
public DbSet Blogs { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer("Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity().OwnsOne(b => b.Owned).ToJson();
}
}

public class Blog
{
public int Id { get; set; }
public string Name { get; set; }

public Owned Owned { get; set; }
}

public class Owned
{
public int OwnedProperty { get; set; }
}
```

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

まず、提供された BlogContext の再現ケースを SQL Server で実行し、JSON owned entity に関係する FromSql クエリで生成された SQL を調査します。この non-composable な CTE クエリを、追加の外側の SELECT projection なしでマテリアライズできれば、作業は完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
csharp, sql
領域
database
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。