0

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.

Jeffrey Rasmussen
  • 367
  • 4
  • 8
  • 21

1 Answers1

1

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;
Andrew
  • 3,648
  • 1
  • 15
  • 29
  • Looks like what I want! Let me check it and I'll get back and mark it as accepted answer! Thank you! – Jeffrey Rasmussen Apr 19 '14 at 18:25
  • 1
    it will be raised for each single object retrieved as a result of query execution... – Andrew Apr 19 '14 at 18:28
  • 1
    I'm not aware of functionality you are developing, but take a note of this remark from documentation to avoid possible bugs `" If an object with the same key value exists in the object context, the Entity Framework will not recreate the object and this event will not be raised. "` – Andrew Apr 19 '14 at 18:35
  • Yes, this is exactly what I need. Is there any way to get object hierarchy? I mean it raised with Entity only and I don't know which entity it was called from. For example I get raised event on Order entity and then on Customer. That would need to .Include("Order"."Customer") and so on.. – Jeffrey Rasmussen Apr 19 '14 at 18:53
  • 1
    I think it is kind of tree - it first materializes parent entity and goes down. you can implement custom logic based on sequence of events raised and check what navigation properties are loaded. – Andrew Apr 19 '14 at 19:06