Search code examples
entity-frameworkdatabase-performance

Check what entities to include


Is there any way to analyze what entities have been loaded during one query request? Currently I'm using lazy loading and it has huge performance impact. So I would want to analyze query somehow and .Include all related objects.


Solution

  • You can handle ObjectMaterialized event of your context.

    ObjectContext.ObjectMaterialized
    

    http://msdn.microsoft.com/en-us/library/system.data.objects.objectcontext.objectmaterialized%28v=vs.110%29.aspx

    PS. If you work with DbContext, you need to retrieve ObjectContext from it first:

    var context = new YourDbContext();
    var adapter = (IObjectContextAdapter)context;
    var objectContext = adapter.ObjectContext;