efcore: "Enumerator failed to MoveNextAsync" when using SingleOrDefaultAsync - after Save
Steps to reproduce
I’m really sorry I don’t have time right now for a full repro, but if this is tricky to track down I can put one together.
When I do a get-or-update:
var rr = await _context.Set<ResourceReference>()
.SingleOrDefaultAsync(r => r.Provider == "azure" && r.ResourceId == resourceId, cancel);
if (rr == null)
{
_context.Set<ResourceReference>().Add(rr = new ResourceReference
{
Provider = "azure",
ResourceId = resourceId
});
}
This works fine.
HOWEVER, if I run the above, THEN SaveChangesAsync
, THEN exactly the same code again (same params), then the exception is triggered.
If I don’t have the save call, it works fine.
System.InvalidOperationException: Enumerator failed to MoveNextAsync.
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleOrDefaultAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
Pretty confused… but even if I create a new context for every call, I still get the error.
Further technical details
EF Core version: 3.0.0
/ 3.1.1
( just checked - still persists)
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10 64-bit
IDE: VS Code
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 10
- Comments: 17 (6 by maintainers)
Doing some digging, this is likely a bad error message, fixed in master (but not release/3.1), whereby if there are >1 results, this message is thrown rather than “sequence contains more than one element”.
If that all adds up, please close! if more info needed, let me know. thanks
Still happening in EF Core 3.1.8 with FirstAsync when sequence contains 0 elements
@CollinHerber change the query or change the data: it’s because we have SingleOrDefault but there was >1 result. The bug is just that the error message is unhelpful
Still happens in efCore 3.1.7 when sequence contains 0 element with SingleAsync
Duplicate of #18742