Is there a reason that the FindAsync() method is omitted from the IDbSet interface? Find is part of the interface, it seems o
Here is how I tackled this in one of our projects:
using System.Threading.Tasks;
namespace System.Data.Entity
{
public static class IDbSetExtensions
{
///
/// If possible asynchronously finds an entity with the given primary key values
/// otherwise finds the entity synchronously.
/// If an entity with the given primary key values exists in the context, then it is
/// returned immediately without making a request to the store. Otherwise, a
/// request is made to the store for an entity with the given primary key values
/// and this entity, if found, is attached to the context and returned. If no
/// entity is found in the context or the store, then null is returned.
///
///
///
/// The values of the primary key for the entity to be found.
/// A task that represents the asynchronous find operation. The task result contains
/// the entity found, or null.
///
public static async Task FindAsync(this IDbSet @this, params object[] keyValues)
where TEntity : class
{
DbSet thisDbSet = @this as DbSet;
if (thisDbSet != null)
{
return await thisDbSet.FindAsync(keyValues);
}
else
{
return @this.Find(keyValues);
}
}
}
}
One might consider wrapping the the Find method in a async-over-sync pattern which would provide offloading (and no scalability as true asynchronous methods do). However the caller must then be aware of this to make sure they aren't going to call methods on the context after calling the FindAsync method that might interfer. Making callers aware of a specific implementation isn't a very good design imho however because it can easily lead to problems. For the OP the IDbSet is a DbSet however so the call will be asynchronous.