New Translate<T> and ExecuteStoreQuery<T> (+ExecuteStoreCommand) on ObjectContext in Entity Framework v4
I don’t know whether it’s somewhere specifically pointed, but the ObjectContext in Entity Framework v4 has two (three) new handy methods. And I like these.
It’s kind of escape hatch similar to DefiningQuery. First method is Translate<T>. It takes DbDataReader and materializes the data back into entities. It’s similar to Materialize method from EFExtensions. If you some code in pure ADO.NET and you don’t have time or resources to redo it in EF (or it’s way easier old way) you can rewire the result into existing objects. I like it. Whenever I’ll feel I need to get dirty (and probably due to performance reasons) I can do it pretty easily.
using (testovaciEntities ent = new testovaciEntities())
{
IDbConnection conn = (ent.Connection as EntityConnection).StoreConnection;
conn.Open();
using (IDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "select * from master";
using (DbDataReader reader = (DbDataReader)cmd.ExecuteReader())
{
MASTER[] result = ent.Translate(reader).ToArray();
Console.WriteLine(result.Length);
}
}
}
The other method is similar and simplifies the process of getting dirty if you simply need to run you fine tuned query with neat and sexy constructs.
It’s ExecuteStoreQuery<T>. This method simply allows you to run any sql command directly in store language (thus you can use all features your database offers) and fetch and materialize back resulting entities. Similar to this is ExecuteStoreCommand which is similar to ExecuteNonQuery from pure ADO.NET. But you can do this without the method easily too, the method is just more convenient.
BTW also note that the Translate method isn’t adding the entities into context, it’s just about fetching and materializing.



There's 4 Comments So Far
June 23rd, 2010 at 16:05
I just wanted to point out there is another use for Translate(reader). EF4 doesn’t natively support multiple result sets. To get around this limitation you can use Translate to interpret multiple result sets.
Of course you can design your app to split up SQL calls, but on certain core/high-frequency/intensive queries returning multiple result sets can be a big performance boost.
Just in case someone else stumbles across this post, info on this behavior is pretty sparse. The easy way is to get EFExtensions from codeplex then write a snippet something like:
DbCommand command = this.CreateStoreCommand(“Comment_GetCommentsforJSON”, CommandType.StoredProcedure, procParams);
using (command.Connection.CreateConnectionScope()) {
using (DbDataReader reader = command.ExecuteReader()) {
var commentSet = this.Translate(reader);
reader.NextResult();
var comments = this.Translate(reader);
reader.NextResult();
var commentVotes = this.Translate(reader);
} //end using (executereader)
} //end using (conscope)
June 23rd, 2010 at 16:06
Note: the generics brackets were removed from the above post. Calls to translate use greater than / less than generics notation.
November 11th, 2010 at 07:58
what is the assembly reference for IDbConnection and EntityConnection
November 11th, 2010 at 08:00
its
using System.Data.EntityClient;
using System.Data;
Share your thoughts, leave a comment!