Skip to content

Instantly share code, notes, and snippets.

@neoGeneva
Created September 7, 2011 09:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neoGeneva/1200123 to your computer and use it in GitHub Desktop.
Save neoGeneva/1200123 to your computer and use it in GitHub Desktop.
Async CTP and Entity Framework
// This code is provided "as-is" without warranty of any kind, either expressed or implied.
public static class AsyncExtensions
{
public static async Task<IEnumerable<T>> AsAsync<T>(this IQueryable<T> source)
where T : EntityObject
{
var query = (ObjectQuery<T>)source;
var cmd = new SqlCommand();
cmd.Connection = (SqlConnection)((EntityConnection)query.Context.Connection).StoreConnection;
cmd.CommandText = query.ToTraceString();
cmd.Parameters.AddRange(query.Parameters.Select(x => new SqlParameter(x.Name, x.Value ?? DBNull.Value)).ToArray());
cmd.Connection.ConnectionString = new SqlConnectionStringBuilder(cmd.Connection.ConnectionString)
{
AsynchronousProcessing = true
}.ToString();
cmd.Connection.Open();
var reader = await Task.Factory.FromAsync<SqlDataReader>(cmd.BeginExecuteReader(), cmd.EndExecuteReader);
return query.Context.Translate<T>(reader);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment