question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullReferenceException when using two Includes, all fine when using either one

See original GitHub issue

EF Core version: 5.0.9 Database provider: Npgsql Target framework: .NET 5.0 Operating system: Windows IDE: Visual Studio 2022 Preview

I’m getting a weird NullReferenceException when using two Includes. Consider this:

public class Fact
    {
        [Key]
        public Guid FactId { get; set; }

        [ForeignKey("Plan")]
        public Guid PlanId { get; set; }
        public virtual APlan Plan { get; set; }

       // Other fields redacted
    }
public class APlan
    {
        protected APlan()
        {
        }

        [Key]
        public Guid PlanId { get; set; }

        [ForeignKey("Communion")]
        public Guid CommunionId { get; set; }
        public virtual Communion Communion { get; set; }

        // Other fields redacted

        [InverseProperty("Plan")]
        public virtual ICollection<Fact> Facts { get; set; }
    }
    public class SinglePlan : APlan
    {
        // Other fields redacted
    }
    public class Communion
    {
        [Key]
        public Guid CommunionId { get; set; }

        [InverseProperty("Communion")]
        public virtual ICollection<APlan> Plans { get; set; }

        // Other fields redacted
    }

And this is the query:

var commonFilter = _context.Plans
                .Include(p => p.Facts)
                .Include(p => p.Communion);

            var singlePlans = await commonFilter
                .Cast<SinglePlan>()
                .ToArrayAsync();

The exception in NullReferenceException, the stacktrace is:

This exception was originally thrown at this call stack:
    Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.BindProperty(Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.EntityReferenceExpression, Microsoft.EntityFrameworkCore.Metadata.IProperty)
    Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.TryBindMember(System.Linq.Expressions.Expression, Microsoft.EntityFrameworkCore.Metadata.MemberIdentity)
    Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(System.Linq.Expressions.MethodCallExpression)
    Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitMethodCall(System.Linq.Expressions.MethodCallExpression)
    System.Linq.Expressions.MethodCallExpression.Accept(System.Linq.Expressions.ExpressionVisitor)
    System.Linq.Expressions.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
    Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitBinary(System.Linq.Expressions.BinaryExpression)
    Npgsql.EntityFrameworkCore.PostgreSQL.Query.Internal.NpgsqlSqlTranslatingExpressionVisitor.VisitBinary(System.Linq.Expressions.BinaryExpression)
    System.Linq.Expressions.BinaryExpression.Accept(System.Linq.Expressions.ExpressionVisitor)
    System.Linq.Expressions.ExpressionVisitor.Visit(System.Linq.Expressions.Expression)
    ...
    [Call Stack Truncated]

The fun part is that if I remove EITHER of theese Includes, the query work. It won’t work only when I use them both.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Aug 20, 2021

Note from triage: we don’t plan to fix this in a servicing release because it only happens when .Cast<>() is used in this way, which is uncommon and for which there is a workaround as @roji describe above. See the release planning process for more information on how we decide what to patch.

1reaction
rojicommented, Aug 20, 2021

If you post your some complete code I may be able to help you further with a workaround; for example, you may be able to get by with a generic function that composes over _context.Set<T>().

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - What is a NullReferenceException, and how do I fix it?
You are trying to use something that is null (or Nothing in VB.NET). This means you either set it to null , or...
Read more >
Object reference not set to an instance of an object
A null reference means that it is trying to access something that doesn't exist. You either forgot to drag something in the editor,...
Read more >
c# - Is it unreasonable to expect Any() *not* to throw a null ...
I suppose you expect NullReferenceException . Any() is an extension method, it should smoothly integrate with the extended object then throwing ...
Read more >
Fix Null Reference Exception in Unity - YouTube
I will teach you when and how one might receive this kind of error ... a NullReferenceException because whenever I left click with...
Read more >
Object Reference Not Set to an Instance of an Object
The good news is that a lot of null reference errors can be avoided by adding additional logic and code to ensure objects...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found