Expression.Constant reference to DbSet causes translation failure

Open
#18,138 15 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp, sql
Domain
database

Research direction

Start from the LINQ query shown in the issue and run it against EF Core 3.0 with MSSQL 2017 or Azure SQL, comparing the generated SQL with EF Core 2.2. The issue is resolved when the DbSet.Any expression translates to a valid EXISTS-style SQL predicate instead of a list of entity objects.

Written by the indexing model from the issue text.

Description

area-query customer-reported

The following query works perfectly fine on EFCore 2.2, but fails on EFCore 3.0:
(running against MSSQL 2017, or Azure SQL)

            var query = from p1 in context.MyEntities
                        where (p1.MasterCIRId == userContext.CIRId)
                           && (p1.IsActive == true)
                           && !context.MyEntities.Any(p2 => p2.EntityKey == p1.EntityKey)
                        select p1;

Running the query on 2.2 translates nicely into a 'where exists ..' clause in the SQL, whilst 3.0 creates a where clause, passing in a list of DbMyEntity classes which logically cannot be translated into valid SQL.

The entity class is pretty straightforward:

public class DbMyEntity()
{
    public virtual ICollection<DbConditionEntityList> ConditionEntityLists { get; set; }

    [ForeignKey("MasterCIRId")]
    public virtual DbCIR MasterCIR { get; set; }

    [Column("MasterCIRId", Order = 1, TypeName = "int")]
    [Required]
    public int MasterCIRId { get; set; }

    [Column("IsActive", Order = 2, TypeName = "bit")]
    [Required]
    public bool IsActive { get; set; }

    [Column("EntityKey", Order = 3, TypeName = "int")]
    [Required]
    public int EntityKey { get; set; }
}
Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.