dotnet / dotnet/efcore

Projections with more than one derived class fail if no properties are projected.

Open
#31,676 0 comments 0 reactions 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

If a projection returns multiple derived classes, and for one more of those, no properties from the entity are projected, an InvalidOperationException is thrown regarding constants in the client projection.

### Include your code

Minimal repro:

```C#
using Microsoft.EntityFrameworkCore;

// This succeeds.
await new TestDbContext().Items.Select(i => new Item1()).ToListAsync();
// As does this.
await new TestDbContext().Items.Select(i => i.Id == 1 ? new Item1 { Id = i.Id} : new Item2 { Id = i.Id}).ToListAsync();
// This fails with "The client projection contains a reference to a constant expression of 'Item1'...."
await new TestDbContext().Items.Select(i => i.Id == 1 ? new Item1() : new Item2()).ToListAsync();

public class TestDbContext: DbContext
{
public DbSet Items { get; set; } = null!;

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlServer("Server=(localdb)");
}
}

public class Item
{
public int Id { get; set; }
}

class Item1:Item
{
}

class Item2:Item
{
}
```

### Include stack traces

```
System.InvalidOperationException: The client projection contains a reference to a constant expression of 'Item1'. This could potentially cause a memory leak; consider assigning this constant to a local variable and using the variable in the query instead. See https://go.microsoft.com/fwlink/?linkid=2103067 for more information and examples.
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.ConstantVerifyingExpressionVisitor.VisitConstant(ConstantExpression constantExpression)
at System.Linq.Expressions.ExpressionVisitor.VisitUnary(UnaryExpression node)
at System.Linq.Expressions.ExpressionVisitor.VisitConditional(ConditionalExpression node)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VerifyNoClientConstant(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_0`1.b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable`1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToListAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at Program.$(String[] args) in C:\Users\Darren\Source\Repos\EFRepro\EFRepro\Program.cs:line 8
at Program.(String[] args)
```

### Include provider and version information

EF Core version: 7.0, 8.0 preview 6 and preview 7
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework:
Reproduced in:
.NET 7.0
.NET 8.0 preview 6
.NET 8.0 preview 7
Operating system: Windows
IDE:
Rider 2023.2.1
dotnet run (8.0.100-preview.7.23376.3)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.