Skip to content

'Nullable object must have a value' on client-side evaluated filter #13655

Closed

Description

@Poltuu

I strongly suspect this to be a duplicate of #13517 and #12951
but I needed to be sure.

System.InvalidOperationException
  HResult=0x80131509
  Message=Nullable object must have a value.
  Source=System.Private.CoreLib
  StackTrace:
   at System.ThrowHelper.ThrowInvalidOperationException_InvalidOperation_NoValue()
   at System.Nullable`1.get_Value()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__17`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at EF.BUG.Program.Main(String[] args) in C:\Sites\RestDrivenDomain.git\EF.BUG\Program.cs:line 68

Steps to reproduce

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using System;
using System.Collections.Generic;
using System.Linq;

namespace EF.BUG
{
    public class Review
    {
        public Guid Id { get; set; }
        public Warning Warning { get; set; }

        public Review()
        {
            Id = Guid.NewGuid();
        }
    }

    public class Warning
    {
        public Guid Id { get; set; }

        public Guid ReviewId { get; set; }
        public Review Review { get; set; }

        public Warning()
        {
            Id = Guid.NewGuid();
        }
    }

    public class DataContext : DbContext
    {
        public DataContext(DbContextOptions<DataContext> options) : base(options) { }

        public DbSet<Review> Reviews { get; set; }
        public DbSet<Warning> Warnings { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var dbName = Guid.NewGuid().ToString();
            var options = new DbContextOptionsBuilder<DataContext>()
                .UseSqlServer($@"Server=(localdb)\mssqllocaldb;Database={dbName};Trusted_Connection=True;ConnectRetryCount=0")
                //.ConfigureWarnings(warnings => warnings.Throw(RelationalEventId.QueryClientEvaluationWarning))
                .Options;

            try
            {
                using (var context = new DataContext(options))
                {
                    context.Database.EnsureCreated();

                    context.Add(new Review { Warning = new Warning() });
                    context.Add(new Review());
                    context.SaveChanges();
                }

                using (var context = new DataContext(options))
                {
                    var elements = new List<Warning> { null };

                    //throws InvalidOperationException
                    //client-side evaluation
                    var result = context.Set<Review>().Where(r => !elements.Contains(r.Warning)).ToList();
                }
            }
            finally
            {
                using (var context = new DataContext(options))
                {
                    context.Database.EnsureDeleted();
                }
            }
        }
    }
}

Further technical details

EF Core version: 2.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.8.7

Activity

  1. ajcvickers commented on Oct 17, 2018

    @ajcvickers
    Contributor

    Notes for triage: confirmed that this throws when client evaluating. However, the most pragmatic fix is to translate cases we can, then re-assess based on 3.0 query changes. For now the workaround it to make the client evaluation explicit:

    var result = context.Set<Review>().AsEnumerable().Where(r => !elements.Contains(r.Warning)).ToList();
  2. added this to the Backlog milestone on Oct 17, 2018
  3. smitpatel commented on Mar 16, 2020

    @smitpatel
    Contributor

    Related #20164

  4. self-assigned this
    on Mar 16, 2020
  5. modified the milestones: Backlog, 5.0.0 on Mar 28, 2020
  6. added 4 commits that reference this issue on Mar 28, 2020
    24718b0
    ba04dd0
    904a105
    b7360ca
  7. added a commit that references this issue on Mar 31, 2020
    bb88898
  8. modified the milestones: 5.0.0, on Mar 31, 2020
  9. modified the milestones: , 5.0.0 on Apr 1, 2020
  10. modified the milestones: 5.0.0, on Apr 20, 2020
  11. modified the milestones: , 5.0.0 on Nov 7, 2020
  12. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions