EntityHooks
EntityHooks copied to clipboard
hook option to check how many times a sql query is run
hi,
Is there a way to use this to check how many times a dbContext asks the database for data? In my integration tests I want to assert that the data access method only asks for data once or twice.
I was hoping for a non generic version of the onLoad method?
Hi. You may do your own IDbHook implementation, for example:
class IncrementDataAccessCountHook : IDbHook
{
public void HookEntry(IDbEntityEntry entry)
{
entitiesMaterializedCount++;
}
}
and attach it using non generic OnLoad
dbContext.OnLoad().Attach(new IncrementDataAccessCountHook())
You may achieve the same by using generic OnLoad specifying object as a generic type.
hi @dombrovsky ,
Thanks for the prompt reply and excellent library. However i tried this and it looks like it only tells me how many entities are being tracked by the DB context as the hook is being called for every single entry rather than DB call.
Yes, load hooks are fired for each entity object when created from data in the data source as part of a query or load operation (as it based on ObjectMaterialized event).
If you need to count number of individual queries to database, that is something outside of the scope EntityHooks can help with. However I recommend looking into EF interceptions. Look at this tutorial for details.