【问题标题】:Accessing ToListAsync() from Custom Repository for SqlQuery in ASPNetBoilerplate从 ASPNetBoilerplate 中的 SqlQuery 的自定义存储库访问 ToListAsync()
【发布时间】:2017-08-03 18:02:55
【问题描述】:

我正在使用 ASP.Net 样板。我已经实现了 IRepository 来创建一个自定义存储库,因此我可以添加一个自定义方法来从存储过程返回数据。但是,无论我如何构建任务、异步或等待组件,我都会收到以下错误:

System.InvalidOperationException: The provider for the source IQueryable doesn't implement IDbAsyncQueryProvider. Only providers that implement IDbAsyncQueryProvider can be used for Entity Framework asynchronous operations. For more details see http://go.microsoft.com/fwlink/?LinkId=287068.

我的存储库如下所示:

namespace myApp.EntityFramework.Repositories
{
    public class TruckRepository : UHaulTrucks.HoustonRepositoryBase<DailyDockQuery>, ITruckRepository
    {

        public TruckRepository(IDbContextProvider<UHaulHoustonDbContext> dbContextProvider) : base(dbContextProvider)
        {

        }

  public IQueryable<DailyDockQuery> GetDailyDockQuery()
        {

           var ret = Context.Database.SqlQuery<DailyDockQuery>("exec Central_DailyDockQuery").AsQueryable();
            return ret;
        }
   }
}

还有我的服务:

public async Task<PagedResultOutput<DailyDockQueryListDto>> GetDailyDockQuery()
        {
            var query = _truckRepository.GetDailyDockQuery();

            var dailyCount = await query.CountAsync();


            var dailies = await query.ToListAsync();

            var dailiesDtos = dailies.MapTo<List<DailyDockQueryListDto>>();

            return new PagedResultOutput<DailyDockQueryListDto>(
                dailyCount,
                dailiesDtos
                );
        }

任何尝试执行异步方法都会产生上述错误,例如:

var dailyCount = await query.CountAsync();

var dailies = await query.ToListAsync();

正如我所提到的,我已经在 Repository 和 Service 中尝试了几种变体,其中包含 Task 和 async 组合,结果相同。我是用这种方式实现自定义存储库的新手,我怀疑虽然我应该从 IRepository 获得 CountAsync 和 ToListAsync 的好处,但我仍然需要在自定义类中实现它们。

非常感谢任何帮助。仍然在这里找到我的路。

【问题讨论】:

  • 失败的不是存储库,而是提供者,你的数据库连接器。

标签: c# entity-framework asynchronous repository-pattern aspnetboilerplate


【解决方案1】:

在 Aspnetboilerplate 中执行存储过程(选择记录)示例代码:

public async Task<List<string>> GetUserNames()
{
    EnsureConnectionOpen();

    using (var command = CreateCommand("GetUsernames", CommandType.storedProcedure))
    {
        using (var dataReader = await command.ExecuteReaderAsync())
        {
            var result = new List<string>();

            while (dataReader.Read())
            {
                result.Add(dataReader["UserName"].ToString());
            }

            return result;
        }
    }
}

我强烈建议您阅读有关在 Aspnetboilerplate 中使用存储过程的文档。这里有你需要的一切:https://aspnetboilerplate.com/Pages/Documents/Articles/Using-Stored-Procedures,-User-Defined-Functions-and-Views/index.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-02
    相关资源
    最近更新 更多