Jan*_*ann 34 c# linq ambiguous entity-framework-core entity-framework-core-3.1
I am using a LINQ query on a DbSet<T>
:
await _dbContext.Users.AnyAsync(u => u.Name == name);
Run Code Online (Sandbox Code Playgroud)
However, the compiler outputs the following error:
Error CS0121: The call is ambiguous between the following methods or properties:
'System.Linq.AsyncEnumerable.AnyAsync<TSource>(System.Collections.Generic.IAsyncEnumerable<TSource>, System.Func<TSource, bool>)' and
'Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.AnyAsync<TSource>(System.Linq.IQueryable<TSource>, System.Linq.Expressions.Expression<System.Func<TSource, bool>>)'
Run Code Online (Sandbox Code Playgroud)
A similar problem also occurs with other LINQ extension methods, like .Where()
.
I am using EF.Core 3.1 and have the System.Linq.Async
package installed. How do I fix this issue?
Jan*_*ann 74
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.
为了解决这个问题,EF.Core 3.1 添加了两个辅助函数AsAsyncEnumerable()
and AsQueryable()
,它们显式返回各自的接口IAsyncEnumerable<TEntity>
or IQueryable<TEntity>
。
通过调用所需的消歧函数来修复给定的代码示例:
await _dbContext.Users.AsQueryable().AnyAsync(u => u.Name == name);
Run Code Online (Sandbox Code Playgroud)
或者
await _dbContext.Users.AsAsyncEnumerable().AnyAsync(u => u.Name == name);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6952 次 |
最近记录: |