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 issuewhen 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:
- Created 3 years ago
- Reactions:25
- Comments:31 (13 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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 😃
This is definitely a breaking change