Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ADO.NET Entities Framework Performance Tips Every Developer Should Know [closed]

What are the performance tips that every ADO.NET EF developer should know about?

Please put each tip per answer and explain why the tip is good (e.g. by minimizing DB roundtrips).

like image 877
Buu Nguyen Avatar asked Jan 24 '09 12:01

Buu Nguyen


People also ask

When should you not use Entity Framework?

One of the biggest reasons not to use Entity Framework Core is that your application needs the fastest possible data access. Some applications do a lot of heavy data operations with very high-performance demands, but usually business applications don't have that high of a performance demand.

Is ADO.NET faster than Dapper?

In short, Dapper.NET is slightly faster than straight ADO.NET.


1 Answers

Use ObjectContext#GetObjectByKey() to retrieve entities by their key instead of using First() (or FirstOrDefault) operator in the LINQ query. The latter will hit the database everytime while the former will search the EF cache (ObjectStateManager to be specific) for the entity first and won't hit the database if the entity with the specified key is found.

References

  • MSDN documentation on ObjectContext#GetObjectByKey
  • GetObjectbyKey in E.F. vs. Querying for a single entity
like image 114
Buu Nguyen Avatar answered Oct 14 '22 23:10

Buu Nguyen