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

NullReferenceException when accessing property of an optional navigation in child projection #15340

Closed
imb590 opened this issue Apr 12, 2019 · 0 comments · Fixed by #17970
Closed
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-bug
Milestone

Comments

@imb590
Copy link

imb590 commented Apr 12, 2019

The following query fails with NullReferenceException. The exception is thrown only on async call; sync version works.

class Program
{
    static void Main(string[] args)
    {
        using (var db = new Context())
        {
            db.Database.EnsureDeleted();
            db.Database.EnsureCreated();

            db.Main.Add(new Main());
            db.SaveChanges();

            var result = db
                .Main
                .Select(m => new
                {
                    Items = m.Dependent != null
                        ? m.Dependent.Items.Select(i => new { m.Dependent.Data }).ToList()
                        : null,
                })
                .FirstOrDefaultAsync()
                .GetAwaiter()
                .GetResult();
        }
    }
}

public class Context : DbContext
{
    public DbSet<Main> Main { get; set; }
}

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

    public OptionalDependent Dependent { get; set; }
}

public class OptionalDependent
{
    [Key]
    public int MainId { get; set; }

    public Main Main { get; set; }

    public bool Data { get; set; }

    public ICollection<Item> Items { get; set; }
}

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

    public int OptionalDependentId { get; set; }

    public OptionalDependent OptionalDependent { get; set; }
}
Exception message: System.NullReferenceException: Object reference not set to an instance of an object.
Stack trace:
   at lambda_method(Closure )
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor._ExecuteAsync[T](IReadOnlyList`1 taskFactories, Func`2 selector)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.AsyncSelectEnumerable`2.AsyncSelectEnumerator.MoveNext(CancellationToken cancellationToken)
   at System.Linq.AsyncEnumerable.FirstOrDefault_[TSource](IAsyncEnumerable`1 source, CancellationToken cancellationToken) in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\First.cs:line 144
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.TaskResultAsyncEnumerable`1.Enumerator.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteSingletonAsyncQuery[TResult](QueryContext queryContext, Func`2 compiledQuery, IDiagnosticsLogger`1 logger, Type contextType)
   at Program.Main(String[] args)

EF Core version: 2.2.4

@ajcvickers ajcvickers added this to the 3.0.0 milestone Apr 12, 2019
@smitpatel smitpatel added the verify-fixed This issue is likely fixed in new query pipeline. label Jun 28, 2019
@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 28, 2019
@smitpatel smitpatel removed their assignment Aug 7, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 3.1.0 Sep 4, 2019
@divega divega assigned maumar and smitpatel and unassigned smitpatel and maumar Sep 11, 2019
@smitpatel smitpatel self-assigned this Sep 12, 2019
smitpatel added a commit that referenced this issue Sep 20, 2019
@smitpatel smitpatel added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed punted-for-3.0 verify-fixed This issue is likely fixed in new query pipeline. labels Sep 20, 2019
smitpatel added a commit that referenced this issue Sep 21, 2019
@ajcvickers ajcvickers modified the milestones: 3.1.0, 3.1.0-preview1 Oct 15, 2019
@ajcvickers ajcvickers removed this from the 3.1.0-preview1 milestone Dec 2, 2019
@ajcvickers ajcvickers added this to the 3.1.0 milestone Dec 2, 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-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants