dotnet / dotnet/efcore

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

Aperta
#36,282 2 commenti 1 reazione 0 assegnatari Vedi su GitHub
area-json area-query
Lingua principale
C#
Stelle
14.8k
Fork
3.4k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

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; }
}
```

Guida per i contributori

Apri la guida per i contributori

Direzione di ricerca

Start by running the supplied BlogContext reproduction with SQL Server and inspect the generated SQL for the FromSql query involving the JSON owned entity. The work is done when this non-composable CTE query can be materialized without the additional outer SELECT projection.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
csharp, sql
Ambito
database
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.