简体   繁体   English

从ASPNetBoilerplate中的SqlQuery从自定义存储库访问ToListAsync()

[英]Accessing ToListAsync() from Custom Repository for SqlQuery in ASPNetBoilerplate

I'm using ASP.Net Boilerplate. 我正在使用ASP.Net Boilerplate。 I have implemented IRepository to create a custom Repository so I could add a custom method for returning data from a Stored Procedure. 我已经实现了IRepository以创建自定义存储库,因此可以添加自定义方法来从存储过程返回数据。 However, no matter how I seem to structure the Task, async or await components I get the following error: 但是,无论我如何构造任务,异步或等待组件,我都会收到以下错误:

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.

My Repository looks like: 我的存储库如下所示:

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;
        }
   }
}

And my service: 而我的服务:

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
                );
        }

The error above will be generated at any attempt to perform an async method, such as: 尝试执行异步方法的任何尝试都会生成上述错误,例如:

var dailyCount = await query.CountAsync();

or 要么

var dailies = await query.ToListAsync();

As I've mentioned I have tried several variations in the Repository and Service with Task and async combinations with the same results. 如前所述,我已经尝试了带有Task和async组合的Repository和Service中的几种变体,它们的结果相同。 I'm new at implementing custom Repositories in this way and suspect although I should be getting the benefit of CountAsync and ToListAsync from IRepository that I still need to implement those in the custom class. 我是通过这种方式实现自定义存储库的新手,尽管我应该从IRepository获得CountAsync和ToListAsync的好处,但我仍然需要在自定义类中实现它们,尽管我对此感到怀疑。

Any help is greatly appreciated. 任何帮助是极大的赞赏。 Still finding my way here. 仍然在这里找到我的方式。

Executing stored procedure (select records) sample code in Aspnetboilerplate: 在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;
        }
    }
}

I highly recommend you to read the document about using stored procedures in Aspnetboilerplate. 我强烈建议您阅读有关在Aspnetboilerplate中使用存储过程的文档。 There's everything you need: https://aspnetboilerplate.com/Pages/Documents/Articles/Using-Stored-Procedures,-User-Defined-Functions-and-Views/index.html 有您需要的一切: https : //aspnetboilerplate.com/Pages/Documents/Articles/使用存储过程-User-Defined-Functions-and-Views / index.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM