Ambiguous call when using LINQ extension method on DbSet

The described problem is caused by using the System.Linq.Async package along with the DbSet<TEntity> class.

Since DbSet<TEntity> implements both IQueryable<TEntity> and IAsyncEnumerable<TEntity>, importing the namespaces System.Linq and Microsoft.EntityFrameworkCore leads to the definition of the conflicting extension methods. Unfortunately, avoiding importing one of them is usually not practicable.

This behavior is present beginning with EF.Core 3.0, and is discussed in this issue.

In order to address this, EF.Core 3.1 adds two auxiliary functions AsAsyncEnumerable() and AsQueryable(), which explicitly return the respective interfaces IAsyncEnumerable<TEntity> or IQueryable<TEntity>.

The given code sample is fixed by calling the desired disambiguation function:

await _dbContext.Users.AsQueryable().AnyAsync(u => u.Name == name);

or

await _dbContext.Users.AsAsyncEnumerable().AnyAsync(u => u.Name == name);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)