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.77k stars 3.19k forks source link

NullReferenceException querying joined object #6347

Closed corradolab closed 8 years ago

corradolab commented 8 years ago

Steps to reproduce

Given these tables

  public class Car
  {
    [Key]
    public string Name { get; set; }
    public string EngineID { get; set; }
    public Engine Engine { get; set; }
  }
  public class Engine
  {
    [Key]
    public string EngineID { get; set; }
    public string Fuel { get; set; }
  }

with these data

insert into Cars values ('Alfa Romeo Giulietta','ENG01')
insert into Cars values ('Audi A4', null)
insert into Engines values ('ENG01', 'petrol')

execute the following query

context.Cars
        .Include(x => x.Engine)
        .Where(x => x.Engine.Fuel.Contains("petrol"))
        .ToList();

The issue

The main issue is the query fails with "System.NullReferenceException: Object reference not set to an instance of an object". Related issues are:

      Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT [x].[Name], [x].[EngineID], [x.Engine].[EngineID], [x.Engine].[Fuel], [e].[EngineID], [e].[Fuel]
      FROM [Cars] AS [x]
      LEFT JOIN [Engines] AS [x.Engine] ON [x].[EngineID] = [x.Engine].[EngineID]
      LEFT JOIN [Engines] AS [e] ON [x].[EngineID] = [e].[EngineID]
      ORDER BY [x].[EngineID]
fail: Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory[1]
      An exception occurred in the database while iterating the results of a query.
      System.NullReferenceException: Object reference not set to an instance of an object.
         at lambda_method(Closure , TransparentIdentifier`2 )
         at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
         at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
         at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
fail: Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware[0]
      An unhandled exception has occurred: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
   at lambda_method(Closure , TransparentIdentifier`2 )
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Rileva.Controllers.CarsController.Get() in C:\Users\clabinaz\documents\visual studio 2015\Projects\Rileva\src\Rileva\Controllers\CarsController.cs:line 26
   at lambda_method(Closure , Object , Object[] )
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionFilterAsync>d__28.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeAsync>d__18.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>d__6.MoveNext()

Further technical details

EF Core version: 1.0 Operating system: Windows 10 Pro Visual Studio version: 2015

maumar commented 8 years ago

Duplicate of https://github.com/aspnet/EntityFramework/issues/5613

Related issues are also duplicates of https://github.com/aspnet/EntityFramework/issues/4588 and https://github.com/aspnet/EntityFramework/issues/6206 respectively