0

The expression that is thrown is:

System.NotSupportedException: 'Unable to create a constant value of type 'CantShowYouThis'. Only primitive types or enumeration types are supported in this context.'

Query that causes an error

context.tableA.RemoveRange(context.tableA.Where(conflict => listOfCantShowYouThis.Select(r => 
r.Id).Contains(conflict.RequestId)));

Solution:

var list = listOfCantShowYouThis.Select(r => r.Id);
context.tableA.RemoveRange(context.tableA.Where(conflict => list.Contains(conflict.RequestId)));

This code is taken from the codebase I'm working with, so I've had to remove the names of classes and such just to stay on the safe side.

From googling a bit I think it is related to the mixing of in-memory data and database data which EF / LINQ-to-entity doesn't like. I might be wrong, but even if I'm not, it is still a very shallow understanding of what is going on. I'd like to understand why my fix works. I know that doing Select won't really do much until it is actually used (by calling ToList() or some other method), because of deferred execution so it strange to me that moving it like this solves the problem.

4
  • Is CantShowYouThis the type of listOfObject and if so you should change it to List<CantShowThis> otherwise it's hard to guess at what your actual error is Jul 1, 2020 at 10:36
  • listOfObject is of type List<CantShowYouThis> I will update my question to clearify this, I could have done a better job there Jul 1, 2020 at 10:45
  • Is this EF or EFCore? Jul 1, 2020 at 13:34
  • We're using EF version 6.1.3 Jul 2, 2020 at 6:11

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy