【问题标题】:Ambiguous call when using LINQ extension method on DbSet<T>在 DbSet<T> 上使用 LINQ 扩展方法时调用不明确
【发布时间】: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 包。我该如何解决这个问题?

【问题讨论】:

    标签: c# linq entity-framework-core ambiguous entity-framework-core-3.1


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2012-06-10
      • 2011-11-08
      • 2013-06-21
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2018-03-06
      • 1970-01-01
      相关资源
      最近更新 更多