EF Include doesn't work on a iterative query

71 views Asked by At

I'm working with C# EF 7.0.2, Z.EntityFrameworkPlus and SQL Server. I have a database that could be grown a lot and I want to make it scalable; To do this, I figure out to work with "chunks" to avoid timeout issues.

I have 4 entities

  1. Products
  2. Attributes
  3. ProductAttribute (n:m)
  4. RequestedProducts (List of user's requests to process products)

I was trying to process the products joining the entities Products and RequestedProducts, querying them by chunks (I'm using Take to avoid use OrderBy function, cuz in the local test that increment a lot the query result time) including ProductAttribute and Attributes entities. Then, when the parallel process ends, I'm using BatchUpdate to change de "Finished" flag so that next query doesn't include processed products (I'd understood that IQueryable doesn't execute the query until is necesary)

The first run of the query it works fine, but the next iteration the entities result doesn't include the ProductAttribute entities

IQueryable<Product> products = this.context.Products
    .Join(
        this.context.RequestedProducts.Where(x=>!x.Finished),
        p => p.Id,
        pp => pp.ProductId,
        (p, pp) => p)
        .Include(x => x.Attributes)
        .ThenInclude(x => x.Attribute)
        .Include(x => x.Classifications);

IQueryable<StepProduct> queryToRun = products
        .Take(productLimit);

while(queryToRun){
    queryToRun
        .AsParallel()
        .WithDegreeOfParallelism(maxDegree)
        .ForAll(product =>
        {
            //Here is the process products code
        }
    this.context.RequestedProducts
            .Where(g => queryToRun.Select(q => q.ProductId).Contains(g.Id))
            .Update(new RequestedProducts(){ Finished=true });
    queryToRun = products        
        .Take(productLimit);
}

I tried to asign the full IQueryable to the queryToRun var, then I tried put the Include sentences in queryToRun and not in products, but it doesn't work. The first time all of the products entities have Attributes, but the next chunk, Attributes ever was empty.

0

There are 0 answers