Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include() does not work if used after Union() #16065

Closed
mbodecek opened this issue Jun 13, 2019 · 4 comments · Fixed by #16249 or #16339
Closed

Include() does not work if used after Union() #16065

mbodecek opened this issue Jun 13, 2019 · 4 comments · Fixed by #16249 or #16339
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@mbodecek
Copy link

mbodecek commented Jun 13, 2019

I have the following data context:

public class Foo
{
    public long Id { get; set; }
}

public class Bar
{
    public long Id { get; set; }
    public Foo Foo { get; set; }
}

public class DataContext : DbContext
{
    public DbSet<Foo> Foos { get; set; }
    public DbSet<Bar> Bars { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(@"data source=(local);initial catalog=IncludeTest;Integrated Security=True");
    }
}

With this setup, I'm not able to use Include after I applied Union:

var list = ctx.Bars
    .Union(ctx.Bars)
    .Include(b => b.Foo)
    .ToList();

// items will have null Foo, even though the associated entity is present

This will work if Include() is used before Union(), but this workaround is less than ideal, when I want the queryable to be reusable and only care about fetching the association in specific cases.

Further technical details

EF Core version: 2.2.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.9.3

@maumar
Copy link
Contributor

maumar commented Jun 13, 2019

related to #6812

@ajcvickers
Copy link
Member

Notes from triage: We plan to implement this in 3.0, but consider:

  • Union (and likely other set operations) must operate on the same shape to be translated. This means that if the first ctx.Bars has an Include, then the second must also do so.
    So this is okay:
var list = ctx.Bars.Include(b => b.Foo)
    .Union(ctx.Bars.Include(b => b.Foo))
    .ToList();

But this will throw:

var list = ctx.Bars.Include(b => b.Foo)
    .Union(ctx.Bars)
    .ToList();
  • It is likely that putting the Include at the end will work in 3.0, although semantically it can be different from two Includes on the two roots.
  • Note that this implies that putting the Include on only the first query root means something different from putting at the end after the two roots are combined into a single query. In other words, this:
var list = ctx.Bars
    .Include(b => b.Foo)
    .Union(ctx.Bars)
    .ToList();

is different from this:

var list = ctx.Bars
    .Union(ctx.Bars)
    .Include(b => b.Foo)
    .ToList();

Assigning to @roji
/cc @smitpatel @divega for anything I missed.

@mbodecek
Copy link
Author

@maumar @ajcvickers Thanks for clarification. It's a fair distinction and in makes sense this does not work with the current in-memory implementation of Union (if maybe surprising for new users coming from EF6).

roji added a commit to roji/efcore that referenced this issue Jun 25, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. dotnet#16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes dotnet#6812
Fixes dotnet#13196
Fixes dotnet#16065
Fixes dotnet#16165
roji added a commit that referenced this issue Jun 25, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. #16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes #6812
Fixes #13196
Fixes #16065
Fixes #16165
@roji
Copy link
Member

roji commented Jun 25, 2019

This works in the new implementation, see test Union_Include introduced in #16249.

roji added a commit to roji/efcore that referenced this issue Jun 26, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. dotnet#16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes dotnet#6812
Fixes dotnet#13196
Fixes dotnet#16065
Fixes dotnet#16165
roji added a commit to roji/efcore that referenced this issue Jun 27, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. dotnet#16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes dotnet#6812
Fixes dotnet#13196
Fixes dotnet#16065
Fixes dotnet#16165
roji added a commit that referenced this issue Jun 27, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. #16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes #6812
Fixes #13196
Fixes #16065
Fixes #16165
roji added a commit to roji/efcore that referenced this issue Jun 27, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. dotnet#16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes dotnet#6812
Fixes dotnet#13196
Fixes dotnet#16065
Fixes dotnet#16165
roji added a commit that referenced this issue Jun 27, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. #16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes #6812
Fixes #13196
Fixes #16065
Fixes #16165
roji added a commit that referenced this issue Jun 28, 2019
* Include/nav rewriting is now supported.
* We now push down to subquery when OrderBy, Take or Skip are applied to set operations, to avoid using the hack where ColumnExpression with no table alias was used. #16244 was opened to track for post-3.0.
* Added missing support for union over subselect projection mappings.
* Added Other type to SetOperationType so that providers can define extra set operations (e.g. PostgreSQL `INTERSECT ALL`, `EXCEPT ALL`).

Completes #6812
Fixes #13196
Fixes #16065
Fixes #16165
roji added a commit that referenced this issue Jun 28, 2019
roji added a commit that referenced this issue Jun 28, 2019
@ajcvickers ajcvickers reopened this Jun 28, 2019
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. query and removed propose-punt labels Jun 28, 2019
roji added a commit that referenced this issue Jul 1, 2019
roji added a commit that referenced this issue Jul 1, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview7 Jul 2, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview7, 3.0.0 Jul 2, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, 3.0.0-preview8 Jul 29, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0-preview8, 3.0.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants