Efcore: "Enumerator failed to MoveNextAsync" when using SingleOrDefaultAsync - after Save

Created on 19 Jan 2020  路  15Comments  路  Source: dotnet/efcore

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:
```c#

        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.SingleOrDefaultAsyncTSource
```

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

closed-duplicate customer-reported

Most helpful comment

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

All 15 comments

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

@maumar to investigate.

What was the resolution to this? I am now getting this as well.

Duplicate of #18742

@CollinHerber change the query or change the data: it鈥檚 because we have SingleOrDefault but there was >1 result. The bug is just that the error message is unhelpful

Hello @kierenj - Yes I understand the bug but our error logs become useless when this bug is present. The bug wasn't breaking my app but affecting my audit and logging tools.

Sorry for the misunderstanding.

ah, yes, of course. I believe this is I still an issue for aspnet core (ef core perhaps?) 3.1..

Still happens in efCore 3.1.7 when sequence contains 0 element with SingleAsync

Still happening in EF Core 3.1.8 with FirstAsync when sequence contains 0 elements

Also still happening in EF Core 3.1.8 when calling SingleOrDefaultAsync when sequence contains 0 elements.

This issue is duplicate of #18742 which has been fixed in 5.0 preview1 release. It will not work for any 3.1.x release.

@smitpatel No chance of getting it back ported?

@esfomeado - The issue is only about the message of the exception. Exception is still thrown since it is error. So there should be no impact on apps unless they are catching exception throwing and trying to match on the error message.

Still happens in efCore 3.1.8 when sequence contains 0 element with SingleAsync

This issue is duplicate of #18742 which has been fixed in 5.0 preview1 release. It will not work for any 3.1.x release.

Was this page helpful?
0 / 5 - 0 ratings