Debugging Entity Framework With ToTraceString

When you develop with Entity Framework, you may need the SQL instruction passed to the SQL engine when your LINQ query is evaluated.

As you can see from the post ToTraceString Method in Entity Framework, both the System.Data.Objects.ObjectQuery and the System.Data.EntityClient.EntityCommand classed implement the ToTraceString() method, whose evaluation returs just the info you need.

An update in EF 6.1 allows to output the generated SQL doing:

   context.Database.Log = Console.Write; 

after having created the context, as explained in Logging and Intercepting Database Operations.

Refresh in Entity Framework

Entity Framework by default caches the objects retured from the database.
This is convenient, for example, for pseudo-static reference tables, that rarely change and you can accept a restart of your application.

But more times you don't want this, this for example to data updated by multiple front-ends!
So, how can you disable Entity Framework caching?

The solution is:

var query = "some entity framework query"
Context.Refresh(RefreshMode.StoreWins, query);
IList<myEntity> myList = myQuery.ToList();

Having called the Context.Refresh(...) static method before ToList(), makes that the latter one handles correctly inserted and deleted entities, other than updated ones.

You can find a bit more details in the following question+answers page: How to refresh ObjectContext cache from db?.