System.InvalidOperationException: Sequence contains no elements — an EF Core
query ended in First() and no row matched. When an empty result is a normal
case, use FirstOrDefault() and check for null; when it is not,
the exception is telling you the data is not what the code assumed.
Its sibling, Sequence contains more than one element, came from
Single() on a filter that matched two rows. Both were captured in the same
project and are shown below.
The error
PS> dotnet run -- first-bad
...
Unhandled exception. System.InvalidOperationException: Sequence contains no elements
at System.Linq.ThrowHelper.ThrowNoElementsException()
at lambda_method46(Closure, QueryContext)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
...
at Program.<Main>$(String[] args) in ...\EfLab\Program.cs:line 72
PS> dotnet run -- single-bad
...
Unhandled exception. System.InvalidOperationException: Sequence contains more than one element
at System.Linq.ThrowHelper.ThrowMoreThanOneElementException()
at System.Linq.Enumerable.TryGetSingle[TSource](IEnumerable`1 source, Boolean& found)
...
at Program.<Main>$(String[] args) in ...\EfLab\Program.cs:line 79
The cuts hide four NuGet audit warnings from dotnet run and EF Core stack
frames.
Why it happens
The table held two customers, Maria Garcia and David Chen, both in Lima. The first query asked for the first customer in Oslo:
var c = db.Customers.First(c => c.City == "Oslo");
First() needs at least one row, and the filter matched none, so it threw. Single() promises exactly one; the filter
c.City == "Lima" matched two rows, so it threw the "more than one element"
variant. In both traces the top frame is in System.Linq, not in the database
provider, and the message is about the number of rows that came back.
The fix
var c = db.Customers.FirstOrDefault(c => c.City == "Oslo");
Console.WriteLine(c is null ? "No customer in Oslo" : c.Name);
PS> dotnet run -- first-fix
...
No customer in Oslo
FirstOrDefault() returned null for the empty result and the code
handled it. In the same project SingleOrDefault() returned null
on the Oslo filter, but on the two Lima rows it threw the same "more than one element"
message as Single(), so for two matches the filter is what needs fixing. Pick the operator by what you expect: First when a row must exist,
FirstOrDefault when it may not, Single when exactly one row must
exist and a second one is a bug worth an exception. For the "more than one element" case,
tighten the filter to a key or a unique column so that one row can match.
How it was reproduced
A console project from dotnet new console -n EfLab on .NET SDK 10.0.403 with
Microsoft.EntityFrameworkCore.Sqlite 10.0.14 and a SQLite file shop.db, seeded
with two fictional customers in Lima. dotnet run -- first-bad and
dotnet run -- single-bad threw the two messages;
dotnet run -- first-fix printed the null branch.
Frequently asked
- Why does EF Core First() throw Sequence contains no elements?
- Because no row matched the filter. In the reproduction First(c => c.City == "Oslo") ran against a table with only Lima customers and threw. Use FirstOrDefault and check for null when no row is a valid outcome.
- What is the difference between Sequence contains no elements and Sequence contains more than one element?
- The first came from First on an empty result. The second came from Single on a filter that matched two rows. Both are InvalidOperationException thrown from System.Linq after the query ran.
- Should I use First or FirstOrDefault in EF Core?
- Use First when a missing row is a bug you want to see as an exception, and FirstOrDefault when an empty result is a normal case that your code handles.
More decoded errors in the Fixes category.