I am working on a project with a very complex entity. The object graph contains approximately 30 objects in a tree with two levels (there are children of children).
The entity is managed in a browser, and sent as Json to a Web API endpoint. The endpoint serializes the JSON into the complex entity, then the repository uses GraphDiff to save the entity.
On first save, the UpdateGraph call takes around 12 seconds, on subsequent calls on the same entity, takes milliseconds.
I assume that the graph is cached per entity i the EF DbContext.
Is there any way to optimize the first call? We have only a few scenarios when we are using GraphDiff, maybe is there a way to prepare the graphs used in teh applicaton.
Thank you for your help.
I solved it by cloning the GraphDiff source code from GitHub and modifying it.
GraphDiff detects dynamically the keys of the entities, and because the way how the dynamic query expressions are generated to retrieve the already persisted objects, the expression are recompiled by EF every time when a new entity (another primary key) is used for the query. So the compiled queries are not cached only if the entity keys were used before. These query compilations took a lot of time in my case (10s).
The source code was modified to allow the injection of the already persisted entity. This means that instead of letting GrahpDiff to retrieve the entity from the DB, it was retrieved in an optimal way and was injected for merging into GraphDiff.