KeyNotFoundException when adding a single projection
See original GitHub issue var result = await
_dbContext.Events
.Select(e => new ScheduledEventInfo()
{
FileNameLarge = e.Images.Any() ? e.Images.First().FileNameLarge : string.Empty,
FileNameMedium = e.Images.Any() ? e.Images.First().FileNameMedium : string.Empty
})
.ToListAsync();
This was working in previous versions. If I remove “FileNameMedium” or “FileNameLarge” then it can translate the SQL but with both FileNameLarge and FileNameMedium it crashes to “KeyNotFoundException: The given key ‘Outer’ was not present in the dictionary.”
Include provider and version information
EF Core version: 5.0.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: 5.0.0 Operating system: windows IDE: (e.g. Visual Studio 2019 16.3)
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Error projecting from a grouped linq query
My goal is to retrieve one image per (parent) event item but without the binary data being included. Using different answers on SO...
Read more >KeyNotFoundException Class (System.Collections.Generic)
A KeyNotFoundException is thrown when an operation attempts to retrieve an element from a collection using a key that does not exist in...
Read more >unhandled exception of type 'System.Collections.Generic. ...
The error message states: “An unhandled exception of type 'System.Collections.Generic.KeyNotFoundException' occurred in mscorlib.dll, additional ...
Read more >C# KeyNotFoundException: Key Not Present in Dictionary
In a C# program, a KeyNotFoundException was thrown. This is likely caused by a lookup done on a key (one that is not...
Read more >Entity Framework latest version - Awesome .NET - LibHunt
... KeyNotFoundException when adding a single projection ... ArgumentException: The collection argument 'propertyNames' must contain at least one element ...
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
I’ve not managed to reproduce exactly the same error message in the demo app, but similar, and the triggers are the same. What I’m getting at the moment is
System.Collections.Generic.KeyNotFoundException: 'The given key 'EmptyProjectionMember' was not present in the dictionary.'
with and without using AutoMapper.I’ve also hit a completely new problem if I remove the subqueries from AutoMapper
Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected with type 'EfCoreOuterBug.TrainingRequest'. Path '[0].CourseInstance.Matches[0].EmployerSite.TrainingRequests'.'
however as far as I can see the models are correctly constructed, and the relationships between them are detected (initially I explicitly set them, but the automatic entity configuration picks them up too).Both with and without AutoMapper, if the
.Include(x => x.Matches).ThenInclude(x => x.EmployerSite)
line is removed, the error is not triggered. It’s not strictly needed for this query, but in my real-world case, I need to include the related entities so that they are populated in the result.Similarly, if either of the 2 subqueries are removed, the
EmptyProjectionMember
error is not triggered (but the unexpected loop detection is).Edit: Incidentally, there is no query output by the logger when the
EmptyProjectionMember
exception is thrown.I generated package from the PR of the fix. And the above reproducible code is fixed for both with/without AM. If anyone else can share their repro code, I can try out those out too.