Skip to content

Nullable object must have a value thrown for a query with DefaultIfEmpty() #30915

Open

Description

@Luigi6821

Hi,
I am getting error "Nullable object must have a value" using the following scenario:

var categories = from c in dbContext.Set<AMOS_ADDRESSCATEGORY>().AsNoTracking()
                                 select new
                                 {
                                     Id = c.ADDRESSCATEGORYID,
                                     Name = c.DESCRIPTION
                                 };

var addresses = (from address in dbContext.Set<AMOS_ADDRESS>().AsNoTracking()
                                 join c in categories on address.ADDRESSCATEGORYID equals c.Id into aC
                                 from category in aC.DefaultIfEmpty()
                                 select new
                                 {
                                     Id = address.ADDRESSID,
                                     Category = category
                                 }).ToArray();

The above scenarion works pefectly using EF Standard and works in EF Core if I change to:

var addresses = (from address in dbContext.Set<AMOS_ADDRESS>().AsNoTracking()
                                 **join c in dbContext.Set<AMOS_ADDRESSCATEGORY>().AsNoTracking()** 
                                on address.ADDRESSCATEGORYID equals c.ADDRESSCATEGORYID into aC
                                 from category in aC.DefaultIfEmpty()
                                 select new
                                 {
                                     Id = address.ADDRESSID,
                                     Category = category
                                 }).ToArray();

Can you help me please?
Thanks in advance
Luigi

EF Core version: 8.0.0-preview.4.23259.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET 6.0
Operating system:
IDE: Visual Studio 2022 17.4

Activity

  1. ajcvickers commented on May 17, 2023

    @ajcvickers
    Contributor

    This issue is lacking enough information for us to be able to fully understand what is happening. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

  2. Luigi6821 commented on May 17, 2023

    @Luigi6821
    Author

    Hi @ajcvickers
    Please find attached a project for reproducing issue.

    ReproducibleNullObject.zip

  3. ajcvickers commented on May 18, 2023

    @ajcvickers
    Contributor

    Note for triage: we don't handle assigning null to the anonymous type after calling DefaultIfEmpty, but it seems like this should work, and it works in LINQ-to-Objects.

  4. Luigi6821 commented on May 19, 2023

    @Luigi6821
    Author

    Hi @ajcvickers
    I believe it has same issue with non anonymous type.
    Example:

    var categories = from c in dbContext.Set<AMOS_ADDRESSCATEGORY>().AsNoTracking()
                             select new Category
                             {
                                 Id = c.ADDRESSCATEGORYID,
                                 Name = c.DESCRIPTION
                             };
    
     var addresses = (from address in dbContext.Set<AMOS_ADDRESS>().AsNoTracking()
                                     join c in categories
                                    on address.ADDRESSCATEGORYID equals c.Id into aC
                                     from category in aC.DefaultIfEmpty()
                                     select new
                                     {
                                         Id = address.ADDRESSID,
                                         Category = category
                                     }).ToArray();
  5. added this to the Backlog milestone on May 26, 2023
  6. Luigi6821 commented on Nov 15, 2023

    @Luigi6821
    Author

    Hi, Has been solved in version 8 ?
    I tested and seems still present..
    Thanks in advance

  7. ajcvickers commented on Nov 15, 2023

    @ajcvickers
    Contributor

    This issue is in the Backlog milestone. This means that it is not fixed in EF Core 8.0. We will re-assess the backlog and consider this item in the upcoming planning. However, keep in mind that there are many other high priority features with which it will be competing for resources. Make sure to vote (👍) for this issue if it is important to you.

  8. Luigi6821 commented on Nov 15, 2023

    @Luigi6821
    Author

    Hi, thanks for prompt reply. I believe that this issue should be with high priority because fails on a simple left join operation.
    Anyway thanks for suggestion

  9. UliPlabst commented on Dec 20, 2023

    @UliPlabst

    I have the same exception using a simple group by statement, I think the root cause might be the same. You can run this in roslyn pad to reproduce.

    #r "nuget: Microsoft.EntityFrameworkCore.Sqlite,8.0.0"
    #r "nuget: Microsoft.EntityFrameworkCore,8.0.0"
    
    using System.ComponentModel.DataAnnotations;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.Extensions.Logging;
    
    await using var ctx = new Context();
    await ctx.Database.EnsureDeletedAsync();
    await ctx.Database.EnsureCreatedAsync();
    
    ctx.Steps.Add(new Step
    {
        Id = 5
    });
    await ctx.SaveChangesAsync();
    
    await ctx.Steps
        .GroupBy(e => new
        {
            e.Planned
        }).ToListAsync();
    
    public class Context : DbContext
    {
        public DbSet<Step> Steps { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlite(Environment.ExpandEnvironmentVariables("DataSource=%USERPROFILE%/Desktop/test_db.sqlite"))
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Step
    {
        [Key]
        public int Id { get; set; }
        public DateTime? Planned { get; set; }
    }
  10. Luigi6821 commented on Feb 14, 2024

    @Luigi6821
    Author
  11. roji commented on Feb 14, 2024

    @roji
  12. Luigi6821 commented on Feb 15, 2024

    @Luigi6821
    Author
  13. 51 remaining items

  14. modified the milestones: Backlog, 11.0.0 on Aug 3, 2026
  15. modified the milestones: 11.0.0, Backlog on Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions