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

NRE in GroupJoin to parent when using query types #14483

Closed
sandersaares opened this issue Jan 22, 2019 · 1 comment
Closed

NRE in GroupJoin to parent when using query types #14483

sandersaares opened this issue Jan 22, 2019 · 1 comment
Labels
area-query closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported type-bug

Comments

@sandersaares
Copy link

I am getting NullReferenceException from GroupJoin.

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , MaterializationContext )
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedEntityShaper`1.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.UnbufferedOffsetEntityShaper`1.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
   at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider._GroupJoin[TOuter,TInner,TKey,TResult](RelationalQueryContext queryContext, IEnumerable`1 source, IShaper`1 outerShaper, IShaper`1 innerShaper, Func`2 innerKeySelector, Func`3 resultSelector)+MoveNext()
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at ConsoleApp13.Program.Main(String[] args) in C:\Source\ConsoleApp13\ConsoleApp13\Program.cs:line 28

This only happens if I am using query types. With entity types it works fine.

Possibly related: #9892

Steps to reproduce

Use migrations to generate the database and seed data.

using Microsoft.EntityFrameworkCore;
using System;
using System.Linq;

namespace ConsoleApp13
{
    internal class Program
    {
        public static readonly Guid ParentWithChildId = new Guid("bcee42c7-ad08-4a43-b9ea-ecb62939666e");
        public static readonly Guid ParentWithoutChildId = new Guid("d65de666-7a3c-49bb-90e2-c780d10832aa");
        public static readonly Guid ChildId = new Guid("5d846ff2-9db8-4a45-a939-3746df161eb4");

        private static void Main(string[] args)
        {
            using (var dc = new DataContext())
            {
                // Works fine if the types are entity types.
                var joinedEntity = dc.Parent
                    .GroupJoin(dc.Child, p => p.Id, c => c.ParentId, (parent, children) => new { parent, children })
                    .ToArray();

                Console.WriteLine($"{joinedEntity.Length} rows.");

                foreach (var row in joinedEntity)
                    Console.WriteLine($"entity row {row.parent.Id} has {row.children?.Count()} children");

                // But if they are query types, fails with NRE.
                var joinedQuery = dc.ParentQuery
                    .GroupJoin(dc.ChildQuery, p => p.Id, c => c.ParentId, (parent, children) => new { parent, children })
                    .ToArray();

                Console.WriteLine($"{joinedQuery.Length} rows.");

                foreach (var row in joinedQuery)
                    Console.WriteLine($"query row {row.parent.Id} has {row.children?.Count()} children");
            }
        }
    }

    public class ParentRow
    {
        public Guid Id { get; set; }
    }

    public class ChildRow
    {
        public Guid Id { get; set; }
        public Guid ParentId { get; set; }
    }

    public class ParentQueryRow
    {
        public Guid Id { get; set; }
    }

    public class ChildQueryRow
    {
        public Guid Id { get; set; }
        public Guid ParentId { get; set; }
    }

    public class DataContext : DbContext
    {
        public DbSet<ParentRow> Parent { get; set; }
        public DbSet<ChildRow> Child { get; set; }

        public DbQuery<ParentQueryRow> ParentQuery { get; set; }
        public DbQuery<ChildQueryRow> ChildQuery { get; set; }

        public DataContext()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            if (!optionsBuilder.IsConfigured)
                optionsBuilder.UseSqlServer("Server=(local);Database=EFCore_Issue_Repro;Trusted_Connection=True;");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ParentRow>()
                .HasData(
                    new ParentRow
                    {
                        Id = Program.ParentWithChildId
                    },
                    new ParentRow
                    {
                        Id = Program.ParentWithoutChildId
                    }
                    );

            modelBuilder.Entity<ChildRow>()
                .HasData(
                    new ChildRow
                    {
                        Id = Program.ChildId,
                        ParentId = Program.ParentWithChildId
                    }
                );

            modelBuilder.Query<ParentQueryRow>()
                .ToView("Parent");
            modelBuilder.Query<ChildQueryRow>()
                .ToView("Child");
        }
    }
}

Further technical details

EF Core version: 2.2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 1809
IDE: Visual Studio 2017 15.9.5

@smitpatel
Copy link
Contributor

Unsupported query as per #17068

@smitpatel smitpatel removed this from the Backlog milestone Nov 22, 2019
@smitpatel smitpatel added closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. and removed punted-for-3.0 labels Nov 22, 2019
@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-out-of-scope This is not something that will be fixed/implemented and the issue is closed. customer-reported type-bug
Projects
None yet
Development

No branches or pull requests

4 participants