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.

Result of left joining a nullable nominal type should be null (i.e. no instance) and not an instance with all null property values

See original GitHub issue

when Blog does not have a Post, following query does not work in 5.0.0-preview.8.* or 6.0.0-* nightly. but works in 5.0.0-preview.7.*


 public class Blog
    {
        public int BlogId { get; set; }
        public string Url { get; set; }

        [NotMapped]
        public Post Post { get; set; }

        public List<Post> Posts { get; set; } = new List<Post>();
    }

    public class Post
    {
        public int PostId { get; set; }

        public string Title { get; set; }

        public string Content { get; set; }

        public int BlogId { get; set; }

        public Blog Blog { get; set; }
    }

// this IQueryable would come from other API.

 var dbPosts = from p in db.Posts
                          // select p;
                         select new Post
                         {
                             PostId = p.PostId,
                             BlogId = p.BlogId,
                             Content = p.Content
                         };

 var query = from blog in db.Blogs
                    join post in dbPosts on blog.BlogId equals post.BlogId into posts
                    from xpost in posts.DefaultIfEmpty()
                    select new Blog
                    {
                         Url = blog.Url,
                         Post = xpost
                   };

Steps to reproduce

I have a repo to reproduce the bug.

https://github.com/skclusive/EFLeftJoinBug

Unhandled exception. System.InvalidOperationException: Nullable object must have a value.
   at lambda_method17(Closure , QueryContext , DbDataReader , ResultContext , SingleQueryResultCoordinator )
   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.Enumerator.MoveNext()

Further technical details

EF Core version: Database provider: (e.g. Microsoft.EntityFrameworkCore.Sqlite) Target framework: (e.g. .NET Core 5.0) Operating system: IDE: (e.g. Visual Studio Code)

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:25
  • Comments:31 (13 by maintainers)

github_iconTop GitHub Comments

25reactions
optikscommented, May 29, 2021

We’ve just hit this after (attempting) to upgrade from 3.1 to 5.0. From what I can tell, this is a significant behavioural change.

Suggestion: Mention this is Breaking changes in EF Core 5.0?

Googling stack traces isn’t much fun 😃

16reactions
Shiko1stcommented, Aug 30, 2021

This is definitely a breaking change

Read more comments on GitHub >

github_iconTop Results From Across the Web

Left Join in Entity Framework on null values
The cast to value type 'System.Int32' failed because the materialized value is null - Either the result type's generic parameter or the query ......
Read more >
c# - Generic Null/Empty check for each property of a class
I want to extend this code to another end that instead of sending the bool result as true or false, I'll clone the...
Read more >
Nulls really do infect everything, don't they?
But to answer the original question: Yes, C suffers from the same effective result, i.e. that you can stick a NULL into any...
Read more >
Joining on NULL values in SQL Server - YouTube
SQL Server prevents rows from being joined together using the regular join syntax if the column values contain NULLs. In today's episode I ......
Read more >
Documentation - Advanced Types
Effectively, null and undefined are valid values of every type. That means it's not possible to stop them from being assigned to any...
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