Repository navigation
Description
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
ajcvickers commentedon Oct 17, 2018
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:
smitpatel commentedon Dec 9, 2019
This is almost fixed. Following piece of code fails when the list of entities contain null in it.
https://github.com/aspnet/EntityFrameworkCore/blob/ab9e076c5de7dff7a4db6a7ec72ecbf82ee91833/src/EFCore/Query/Internal/EntityEqualityRewritingExpressionVisitor.cs#L1026-L1037
cc: @roji
smitpatel commentedon Mar 16, 2020
Related #20164
Query: Rewrite Entity Equality during translation phase
Query: Rewrite Entity Equality during translation phase
Query: Rewrite Entity Equality during translation phase
Query: Rewrite Entity Equality during translation phase
Query: Rewrite Entity Equality during translation phase