I'm fetching movie meta data from and API and adding them to database using entity framework using the following code
This code this taking upto 20% CPU when called for one movie. I'm using one context for one insert. Add() and DBSet.Local.Where(..) methods are the most expensive here. How can I optimize the code to reduce CPU.
Update: On code profiling I found DBSet.Local.Where(..) is where the CPU is eating up. My reason to call DBSet.Local.Where is to find out if I've already added that object (person) so that I don't re-add it. Thoughts?
This Movies.Where(My => My.ID == Movie.Id).FirstOrDefault()
can be optimize by calling Movies.Find(Movie.Id)
.
Finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. https://msdn.microsoft.com/en-us/library/gg696418(v=vs.113).aspx
I don't see any optimizations for the Add()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.