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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

23reactions
kierenjcommented, Jan 19, 2020

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

12reactions
Davide-DDcommented, Sep 11, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why does this error occur when using SingleAsync?
SingleOrDefaultAsync -> means that it will throw if two or more entries are found. Asynchronously returns the only element of a sequence ...
Read more >
EntityFrameworkQueryableExten...
Asynchronously returns the only element of a sequence, or a default value if the sequence is empty; this method throws an exception if...
Read more >
Microsoft.Data.SqlClient.SqlException (0x80131904): Invalid ...
Web)' Failed executing DbCommand (10ms) ... InitializeReaderAsync(AsyncEnumerator enumerator ... MoveNextAsync() at Microsoft.
Read more >
Timeout while creating tenant #4453 | Support Center
Given that the problem seems to be a command timeout, I have tried setting the command timeout to something like 5 minutes using...
Read more >
There is already an open DataReader associated ... - YouTube
An unhandled exception occurred while processing the request. InvalidOperationException: There is already an open DataReader associated with ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found