Skip to main content

Fixed: "error CS1929: 'DbSet<Category>' does not contain a definition for 'FirstOrDefaultAsync'" in .NET 10

· 5 min read
Jagdish Kumawat
Founder @ Dewiride

The build stopped with error CS1929: 'DbSet<Category>' does not contain a definition for 'FirstOrDefaultAsync' and the best extension method overload 'AsyncEnumerable.FirstOrDefaultAsync…' requires a receiver of type…. ToListAsync failed with a different message, CS0411. On .NET 10, both mean the same simple thing: the file is missing using Microsoft.EntityFrameworkCore;.

What I Was Doing​

I was writing endpoints in a .NET 10 Web API with EF Core 10, in a new file, with these using lines at the top:

src/Dewiride.Inventory.Api/Features/Categories/CategoryEndpoints.cs (excerpt)
using Dewiride.Inventory.Data;
using Dewiride.Inventory.Data.Entities;
using Microsoft.AspNetCore.Http.HttpResults;

namespace Dewiride.Inventory.Api.Features.Categories;

And I built the solution:

Terminal
dotnet build

What I Expected​

Build succeeded.

What I Got​

Output (one of each kind, with the paths shortened)
CategoryEndpoints.cs(112,30): error CS1929: 'DbSet<Category>' does not contain a definition for 'FirstOrDefaultAsync' and the best extension method overload 'AsyncEnumerable.FirstOrDefaultAsync<CancellationToken>(IAsyncEnumerable<CancellationToken>, Func<CancellationToken, bool>, CancellationToken, CancellationToken)' requires a receiver of type 'System.Collections.Generic.IAsyncEnumerable<System.Threading.CancellationToken>'
CategoryEndpoints.cs(69,30): error CS1929: 'IQueryable<CategoryResponse>' does not contain a definition for 'FirstOrDefaultAsync' and the best extension method overload 'AsyncEnumerable.FirstOrDefaultAsync<CancellationToken>(IAsyncEnumerable<CancellationToken>, Func<CancellationToken, bool>, CancellationToken, CancellationToken)' requires a receiver of type 'System.Collections.Generic.IAsyncEnumerable<System.Threading.CancellationToken>'
CategoryEndpoints.cs(59,14): error CS0411: The type arguments for method 'AsyncEnumerable.ToListAsync<TSource>(IAsyncEnumerable<TSource>, CancellationToken)' cannot be inferred from the usage. Try specifying the type arguments explicitly.
CategoryEndpoints.cs(43,16): error CS0246: The type or namespace name 'DbUpdateException' could not be found (are you missing a using directive or an assembly reference?)

A terminal where dotnet build fails with CS1929 for FirstOrDefaultAsync pointing at AsyncEnumerable, CS0411 for ToListAsync, and CS0246 for DbUpdateException.

What I Checked​

Where AsyncEnumerable comes from. My code never mentions it. It's a class in System.Linq, and it's part of .NET 10 itself, with LINQ methods for IAsyncEnumerable. ImplicitUsings brings in System.Linq for every file, so its FirstOrDefaultAsync and ToListAsync are always available. So those were the only methods with those names the compiler could see.

Which line is the honest one. CS0246 for DbUpdateException says it plainly: "are you missing a using directive". That class lives in Microsoft.EntityFrameworkCore, and so do EF Core's own FirstOrDefaultAsync and ToListAsync, in EntityFrameworkQueryableExtensions.

The using list. There it was: no using Microsoft.EntityFrameworkCore;.

Why It Happens​

FirstOrDefaultAsync on a DbSet is an extension method, so the compiler only finds it if its namespace is imported. Without EF Core's namespace, the compiler keeps looking and finds System.Linq.AsyncEnumerable's method with the same name. That one wants an IAsyncEnumerable, and a DbSet doesn't fit in the way it needs, so the message describes that mismatch, not the missing line. Before .NET 10, the same mistake gave a plainer "does not contain a definition" error, because this class wasn't part of .NET.

The Fix​

Add the namespace:

src/Dewiride.Inventory.Api/Features/Categories/CategoryEndpoints.cs (excerpt)
using Dewiride.Inventory.Data;
using Dewiride.Inventory.Data.Entities;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.EntityFrameworkCore;

There's no need to add a package: the project already gets EF Core through the Data project.

After the Fix​

Output
Build succeeded.
0 Warning(s)
0 Error(s)

A terminal where dotnet build succeeds with no warnings and no errors.

All the errors went away with that one line, since they all had the same cause.

Common Mistakes​

MistakeWhat happensDo this instead
Adding type arguments, as CS0411 suggestsMore errors, because the method is the wrong oneAdd the using
Installing the System.Linq.Async packageIt doesn't help a DbSet, and can add conflictsAdd the using
Calling .AsAsyncEnumerable() to satisfy the messageIt compiles, but it isn't what you wantedAdd the using
Adding the EF Core package to the API projectNot needed when a referenced project has itAdd the using

Troubleshooting​

SymptomLikely causeFix
CS1929 naming AsyncEnumerable.FirstOrDefaultAsyncusing Microsoft.EntityFrameworkCore; is missingAdd it
CS0411 naming AsyncEnumerable.ToListAsyncThe sameThe same
CS0246 for DbUpdateExceptionThe sameThe same
The same errors, with the using in placeThe project doesn't reference EF Core at allReference the Data project, or add the package
Results<…> gives CS0308using Microsoft.AspNetCore.Http.HttpResults; is missingAdd it

FAQ​

Can I import it everywhere?​

Yes: add <Using Include="Microsoft.EntityFrameworkCore" /> to the project file. It's a matter of taste. A file-level using makes it clear which files touch the database.

Why don't IDE suggestions always offer it?​

With another method of the same name in view, the editor may not treat the method as missing, so it may not suggest the namespace.

Conclusion​

On .NET 10, error CS1929 … 'AsyncEnumerable.FirstOrDefaultAsync' and CS0411 for ToListAsync usually mean using Microsoft.EntityFrameworkCore; is missing from the file. The compiler found System.Linq.AsyncEnumerable's methods instead, and complained about those. Add the one line.

Additional Resources​

Stay Updated

Subscribe to our newsletter for the latest tutorials, tech insights, and developer news.

By subscribing, you agree to our privacy policy. Unsubscribe at any time.