Can I clone an IQueryable to run on a DbSet for another DbContext?

async-await c# dbcontext entity-framework-6

Question

Suppose I have constructed an argument using conditional logic in a number of phases.IQueryable<T> example, we'll usequery .

I want to call in order to acquire a page of data and a count of all the entries.query.CountAsync() and query.Skip(0).Take(10).ToListAsync() . Because they both attempt to conduct a query on the same database, a race situation results if I try to call them back-to-back.DbContext both at once. This is prohibited:

"A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe."

I don't want to begin the second before even "waiting" for the first. I want to send both inquiries out as soon as I can. The only way to do this is to run them from separate DbContexts. It seems absurd that I could have to construct the full query (or two, or three) side by side by beginning with various examples ofDbSet . Is it possible to replicate or change anIQueryable<T> (maybe not that interface, but its underlying implementation) so that I may have a single copy that functions onDbContext A further one that will function onDbContext "B", such that both queries may run concurrently? Simply put, I don't want to have to recreate the query X times from start in order to execute it on X contexts.

1
6
6/28/2018 5:18:45 PM

Popular Answer

ZZZ_tmp
3
6/28/2018 5:28:31 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow