在 DbSet<T> 上使用 LINQ 扩展方法时调用不明确

Posted

技术标签:

【中文标题】在 DbSet<T> 上使用 LINQ 扩展方法时调用不明确【英文标题】:Ambiguous call when using LINQ extension method on DbSet<T> 【发布时间】:2020-02-22 00:38:12 【问题描述】:

我在 DbSet&lt;T&gt; 上使用 LINQ 查询:

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

但是,编译器会输出以下错误:

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>>)'

其他 LINQ 扩展方法也会出现类似问题,例如 .Where()

我正在使用 EF.Core 3.1 并安装了 System.Linq.Async 包。我该如何解决这个问题?

【问题讨论】:

【参考方案1】:

所描述的问题是由使用System.Linq.Async 包和DbSet&lt;TEntity&gt; 类引起的。

由于DbSet&lt;TEntity&gt; 实现了IQueryable&lt;TEntity&gt;IAsyncEnumerable&lt;TEntity&gt;,因此导入命名空间System.LinqMicrosoft.EntityFrameworkCore 会导致定义冲突的扩展方法。不幸的是,避免导入其中一个通常是不切实际的。

此行为从 EF.Core 3.0 开始出现,并在 this issue 中进行了讨论。

为了解决这个问题,EF.Core 3.1 添加了两个辅助函数AsAsyncEnumerable()AsQueryable(),它们显式返回各自的接口IAsyncEnumerable&lt;TEntity&gt;IQueryable&lt;TEntity&gt;

通过调用所需的消歧函数来修复给定的代码示例:

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

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

【讨论】:

以上是关于在 DbSet<T> 上使用 LINQ 扩展方法时调用不明确的主要内容,如果未能解决你的问题,请参考以下文章

EF查询 linq写法 DbContext.DbSet<TEntity>

带有 DbSet 的 LINQ ToListAsync 表达式

NSubstitute DbSet / IQueryable<T>

FakeItEasy DbSet / IQueryable<T> - 实体框架 6

实体框架和DbSet

在 EF6 中是不是有可能有一个 DbSet<T> 仅存在于内存中,而其余的是真实表?