-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
EFCore 2.1 - System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.EntityFrameworkCore.Extensions.Internal.EFPropertyExtensions.CreateEFPropertyExpression #12531
Comments
What's the object, what's the query, what's the automapper mapping profile set up? |
I've done some further investigation and it seems that all of the classes this are failing with have a property called |
OK I have managed to reproduce this. Seems to be some combination of a Can see repro here: https://github.com/hisuwh/EFAutomapperBugRepro |
Worth noting that I tried without the group by clause also and this works fine so it seems to be some combination of these things |
This seems similar: #12534 |
That issue does share the idea of having a .First() within a select, though it is strange with my issue that removing the Type property from the projection fixes the issue. |
The issues appear to have the same underlying cause. Specifically the issue is seen when accessing a collection of a child model within a .Select on a parent model. Removing the Type obviously stops the above condition from being true. You will also find no failure without the .GroupBy, because accessing a singular navigational property is not flawed. e.g. Type.Name Edit: |
@smitpatel to follow up. |
For further investigation, we need repro code which demonstrate the issue without using AutoMapper, OData libs and pure EF Core query. |
@smitpatel removing the I have tried both of these approaches: // 1:
return queryable
.GroupBy(t => t.TypeId)
.Select(g => new ThingModel
{
Id = g.First().Id,
Name = g.First().Name,
TypeId = g.First().TypeId,
Type = new ThingTypeModel
{
Id = g.First().Type.Id,
Name = g.First().Type.Name
}
});
// 2:
return queryable
.GroupBy(t => t.TypeId)
.Select(g => g.First())
.Select(t => new ThingModel
{
Id = t.Id,
Name = t.Name,
TypeId = t.TypeId,
Type = new ThingTypeModel
{
Id = t.Type.Id,
Name = t.Type.Name
}
}); But neither reproduced the issue. So it must be something specific about how automapper is calling Entity Framework. This issue only presented upon upgrading Entity Framework Core to 2.1 |
Maybe my issue can help shed some light? e.g. These were not: The SQL that was generated was missing the required JOINs. I found this after I noticed adding .ToList() before the .Select() caused the query to execute correctly. So I compared the SQL that was generated by the "faulty" code, to the SQL generated after adding .ToList() before the .Select(). I wonder if AutoMapper is causing a second JOIN, or a second entity step for some reason? The generated SQL should show if this is the case. I also wonder if adding that .ToList() will have the same effect here? This might also help decide whether these are being caused by the same underlying issue. |
@MattOG its not generating SQL though as its throwing the exception before that? |
EF Triage: We are still hoping that someone can provide a repro for this. |
Try again... |
For the original exception message Duplicate of #12738 |
I have just upgraded an existing project to EntityFrameworkCore 2.1
A query that was previously working is now throwing a null reference exception. I have put calls to
.ToList()
at various points through building up my query and it seems to be something to do with the call to automapperProjectTo
.This doesn't happen for all my queries though.
This is the exception I am getting:
Further technical details
EF Core version: 2.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.7.4)
The text was updated successfully, but these errors were encountered: