Reputation: 1118
In our project we used EF Core 2.2.6. But after upgrading to EF Core 3.1, the following piece of code stopped compiling and giving an error
var offerTask = DbContext.Offers.FindAsync(offerId);
var priceTask = DbContext.VendorPrices
.Where(x => x.OfferId == offerId && ...)
.ToListAsync(cancellationToken);
await Task.WhenAll(offerTask, priceTask);
Error CS1503
Argument 1: cannot convert from 'System.Threading.Tasks.ValueTask' to 'System.Threading.Tasks.Task'
I found out that in new version of EF Core, FindAsync
method returns ValueTask
(though in older version it returned Task
), and this is causing the compilation error.
What is the workaround for this issue? Should I just use .AsTask()
?
Upvotes: 0
Views: 310
Reputation: 3055
Ef core 3.1 breaking changes :
Why
This change reduces the number of heap allocations incurred when invoking these methods, improving general performance. Applications simply awaiting the above APIs only need to be recompiled.
Mitigations
Applications simply awaiting the above APIs only need to be recompiled -no source changes are necessary. A more complex usage (e.g. passing the returned
Task
toTask.WhenAny()
) typically require that the returnedValueTask<T>
be converted to aTask<T>
by callingAsTask()
on it. Note that this negates the allocation reduction that this change brings.
Solution:
await Task.WhenAll(offerTask, priceTask).AsTask();
Upvotes: 0