NullReferenceException bug with nested LINQ query
Problem:
Left join with inner query: causes this excepion:
Exception message: Stack trace:
System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , QueryContext , TransparentIdentifier2 )
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper.TypedProjectionShaper3.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.CompositeShaper.TypedCompositeShaper5.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.ProjectionShaper.TypedProjectionShaper3.Shape(QueryContext queryContext, ValueBuffer& valueBuffer)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.BufferlessMoveNext(DbContext _, Boolean buffer)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor1.EnumeratorExceptionInterceptor.MoveNext()
at System.Collections.Generic.LargeArrayBuilder1.AddRange(IEnumerable1 items)
at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at EfLinqBug.Program.Main(String[] args) in C:\Users\LEAL1\source\repos\EfLinqBug\Program.cs:line 80
Steps to reproduce
I've created a test console EF project with two tables Customer & Order which shows the problem. It seems to be just selecting the OrderId in the nested linq causes the exception. If I change it to OrderId.ToString(), it works....It doesn't seem to like the integer for some reason. Also switching to the in-memory provider the exception doesn't happen.
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
namespace EfLinqBug
{
class Program
{
public class MyDbContext: DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
//optionsBuilder.UseInMemoryDatabase("Test");
optionsBuilder.UseSqlServer("Server=localhost
[EfLinqBug.zip](https://github.com/aspnet/EntityFrameworkCore/files/3408454/EfLinqBug.zip)
[EfLinqBug.zip](https://github.com/aspnet/EntityFrameworkCore/files/3408468/EfLinqBug.zip)
;Database=EfLinqBug;Trusted_Connection=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>(entity =>
{
entity.HasKey(e => e.CustomerId);
});
modelBuilder.Entity<Order>(entity =>
{
entity.HasKey(e => new { e.CustomerId, e.OrderId });
entity.HasOne(d => d.Customer)
.WithMany(p => p.Orders)
.HasForeignKey(d => d.CustomerId);
});
}
public virtual DbSet<Customer> Customers { get; set; }
public virtual DbSet<Order> Orders { get; set; }
}
public class Customer
{
public int CustomerId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public virtual ICollection<Order> Orders { get; set; } = new List<Order>();
}
public partial class Order
{
public int CustomerId { get; set; }
public int OrderId { get; set; }
public virtual Customer Customer { get; set; }
}
static void Main(string[] args)
{
var context = new MyDbContext();
if (!context.Customers.Any())
{
context.Customers.Add(new Customer { CustomerId = 1, Code = "aaf", Name = "Cool cat" });
}
context.SaveChanges();
var query = (
from c in context.Customers
join sq in (from ro in context.Orders
select new
{
ro.OrderId, // problem line
ro.Customer.Code,
}) on c.Code equals sq.Code into resultJoin
from r in resultJoin.DefaultIfEmpty()
select new
{
c.CustomerId,
});
Console.WriteLine(query.ToArray().Count());
}
}
}
Further technical details
EF Core version: netcoreapp2.2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.2.6 Operating system: IDE: (e.g. Visual Studio 2017 15.4)
This should work in current
I just tried it with .NetCore 3.0.0-preview.18572.1 and it failed with the same error:
Microsoft.EntityFrameworkCore Version="3.0.0-preview.18572.1 Microsoft.EntityFrameworkCore.Design Version="3.0.0-preview.18572.1 Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview.18572.1
Can you try preview6 or higher?
Yep it's fixed in the latest version.