Created
September 7, 2011 09:11
-
-
Save neoGeneva/1200123 to your computer and use it in GitHub Desktop.
Async CTP and Entity Framework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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