Tips and Trips to increase ef core performance

There are many articles about how to boost #ef_core performance and how to reduce the amount of resources is needed by ef core to do same work, But theory and reality are total two different worlds.

In @accent-design-group-ltd we came across a lack of memory during the processing of big data which included reading data from an #API, Validating that data, #Store it in #postregre, and Cache by #redis.

To fix that odd issue we decided to improve 2 main buttle necks of that process :

1- Ef core improvement (This article)

2- How is data being serialized to bytes to store in #Redis #cache

Let’s jump to practice and see what we did to boost ef core side and save memory alot

1-Untrack records changes in ef core

queryable.AsNoTrackingWithIdentityResolution();

2-Use Ef core 5 new feature to clear context, It will clear tracked entities during any CRUD transactions and save a bit of memory. Notice that it would depend on your use case, in some of the use cases it worth keeping tracked records

this.context.ChangeTracker.Clear()

3- Use another ef core 5 feature to bulk add asynchronously

DbSet.AddRangeAsync

Obviously, after untracked changes in ef core, some of the #integration_tests was broken, to overcome that you might need to include navigation properties or take care of entity states specially Many-to-Manies

https://docs.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key
https://docs.microsoft.com/en-us/ef/core/change-tracking/change-detection
https://docs.microsoft.com/en-us/ef/core/saving/cascade-delete

Leave a Comment