运行多个Async / Await调用时的DBContext和AsNoTracking

时间:2019-04-16 10:15:28

标签: c# entity-framework dbcontext

我正在尝试将“ AsNoTracking”添加到一些数据库调用中,但是我一直遇到问题。

我有以下代码:

    public class MyRepository
{
    private readonly CustomsDataContext _context;

    public AggregatedCustomsDataRepository(CustomsDataContext context)
    {
        _context = context;
    }

    public async Task<List<AggregatedCustomsDataRow>> GetAggrigatedDataRowsCompanyNumber(string companyNumber, DateTime fromDate, DateTime toDate)
    {
        return await _context.AggrigatedDataRows
            .Where(x => x.companyNumber == companyNumber && 
                        fromDate <= x.FromDate && toDate >= x.ToDate)
            .AsNoTracking()
            .ToListAsync();
    }

    public async Task<IEnumerable<AggregatedCustomsDataRow>> GetAggregatedCustomsDataForPeriod(DateTime fromDate, DateTime toDate)
    {
        return await _context.AggrigatedDataRows
            .Where(x => fromDate <= x.FromDate && toDate >= x.ToDate)
            .AsNoTracking()
            .ToListAsync();
    }

    public async Task<IEnumerable<AggregatedCustomsDataRow>> GetAggrigatedDataRowsLastYear()
    {
        var periodFrom = DateTime.UtcNow.AddMonths(-12);
        var periodTo = DateTime.UtcNow;

        return await _context.AggrigatedDataRows
            .Where(x => x.FromDate >= periodFrom && x.ToDate <= periodTo)
            .AsNoTracking()
            .ToListAsync();
    }   
}

public MyController
{
    MyRepository _repo;
    MyOtherRepository _otherRepo;

    public async Task<IEnumerable<OutgoingDataRow>> DoWork(DateTime fromDate, DateTime toDate)
    {
        var allData = _repo.GetAggregatedCustomsDataForPeriod(fromDate, toDate);
        var allDataCompanyA = _repo.GetAggrigatedDataRowsCompanyNumber("CompA", fromDate, toDate);
        var allDataCompanyB = _repo.GetAggrigatedDataRowsCompanyNumber("CompB",fromDate, toDate);
        var allDataLastYear = _repo.GetAggrigatedDataRowsLastYear();

        Task.WaitAll(allData, allDataCompanyA, allDataCompanyB);

        return (await _otherRepo.GetAllProductTypes()).Select(ProductType) =>
        {
            var tempA = allData.Result.Where(x=>x.ProductType.Code = ProductType.Code)
            var tempB = allDataCompanyA.Result.Where(x=>x.ProductType.Code = ProductType.Code)
            var tempC = allDataCompanyB.Result.Where(x=>x.ProductType.Code = ProductType.Code)
            var tempD = allDataLastYear.Result.Where(x=>x.ProductType.Code = ProductType.Code)

            // Use data from all tasks to build return object
            return new OutgoingDataRow
            {
                //...
            }
        }
    }
}

如果删除AsNoTracking,代码可以正常工作,但是如果添加它们,则会出现一些对我来说没有多大意义的错误。

在一个函数中,我得到: System.ArgumentOutOfRangeException:'索引超出范围。必须为非负数并且小于集合的大小。 参数名称:index'

,另外我得到: System.InvalidCastException:“无法将类型为System.Data.ProviderBase.DbConnectionClosedConnecting的对象转换为类型为System.Data.SqlClient.SqlInternalConnectionTds”。

我尝试将MultipleActiveResultSets = True设置为True,但没有任何作用。

我尝试为每个数据库调用使用一个新的DbContext,但我无法完全理解这是否是一个好主意。

我在做错什么以及如何解决?

0 个答案:

没有答案