dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.75k stars 3.18k forks source link

.Include() with Self-Join / Coalesce #3101

Closed brent-russell closed 8 years ago

brent-russell commented 9 years ago

Beta 7 and Beta 6.

This might encompass two separate issues. The Include() method is problematic with the following query:

var query = from eVersion in _dbContext.Entities
            join eRoot in _dbContext.Entities
                on eVersion.RootEntityId.GetValueOrDefault() equals eRoot.Id
            into RootEntities
            from eRootJoined in RootEntities.DefaultIfEmpty()  // left outer join
            select eRootJoined ?? eVersion;

If query = query.Include(e => e.Children); is used, the coalesce expression eRootJoined ?? eVersion cannot be bound.

If Include is added to the second or both of the _dbContext.Entities lines, then a null reference exception occurs.

If Include is added to the first _dbContext.Entities line, the query runs, but does not include the children.

public class Entity
{
    public Entity()
    {
        this.Children = new Collection<Child>();
    }

    public int Id { get; set; }

    public int? RootEntityId { get; set; }

    public Entity RootEntity { get; set; }

    public ICollection<Child> Children { get; set; }
}

Where RootEntityId is a self-referential relation on the Entity table.

maumar commented 9 years ago

@brent-russell could you also share a model you are using?

brent-russell commented 9 years ago

@maumar edited original comment to include model info.

maumar commented 8 years ago

poaching this

maumar commented 8 years ago

blocked by #4547

maumar commented 8 years ago

blocked by #4925

maumar commented 8 years ago

clearing milestone for retriage

rowanmiller commented 8 years ago

@maumar where are we at with this issue?

rowanmiller commented 8 years ago

@maumar ping đŸ˜„

maumar commented 8 years ago

@rowanmiller fix is almost ready

maumar commented 8 years ago

Fixed in 7bdf5818b10389588d74ecc03ba766fe24850298