EntityHooks icon indicating copy to clipboard operation
EntityHooks copied to clipboard

hook option to check how many times a sql query is run

Open chinwobble opened this issue 7 years ago • 3 comments

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?

chinwobble avatar Mar 19 '17 11:03 chinwobble

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.

dombrovsky avatar Mar 20 '17 18:03 dombrovsky

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.

chinwobble avatar Mar 20 '17 22:03 chinwobble

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.

dombrovsky avatar Mar 22 '17 19:03 dombrovsky