Freigeben über


ObjectQuery.ToTraceString() and possibilities...

So in Beta3 of the EntityFramework (due out very soon) the team added a new function to ObjectQuery<T> that allows you to get the native Query that would be evaluated if you enumerated. I.e. if you are using the System.Data.SqlClient that would be the TSQL.

This is very handy because it means you can see what is going on without reverting to Profiler. It is important to note that ToTraceString() doesn't actually execute the query, it simply does the clientside conversion from an ObjectQuery to a native query.

You can also do some pretty creative things once you know the SQL, for example how about making your own bulk update framework over the top of the Entity Framework, something like this:

var v = from o in ctx.Orders
        where o.ShipName == "Titanic"
        select o;

int effected = v.Update(o => new Orders(){ShipName = "Queen Mary"});

Console.WriteLine("Moved {0} orders from the Titanic to the Queen Mary", effected);

Notice no objects are being loaded from the Database in the above code, it is executing completely in the database... ;)

I'll show you how you can implement this in my next post...

Comments