RajasekharwhY / Build-your-muscle

Learning concepts and sample practice code - C#, MVC, SQL etc..
2 stars 0 forks source link

Diff between AsNoTracking(),GetQueryable() #10

Open RajasekharwhY opened 5 years ago

RajasekharwhY commented 5 years ago

Write example to use .Include() to get the related objects, with and without .Include() method. and try to get the results and see what difference it makes.....

Datacontext or ObjectContext are keeps track of all entities instantiates. DbContext in Entity Framework is responsible for tracking the changes made on the entity or object, so the correct update is done to the database when the SaveChange() method of context is called. When we retrieve entities using an object query, the Entity Framework puts these entities in a cache and tracks whatever changes are made on these entities until the savechanges method is called. Entity Framework tracks the query results that return entity types.

Sometimes we do not want to track some entities because the data is only used for viewing purposes and other operations such as insert, update and delete are not done. For example, the view data in a read-only grid.

In the scenario described above, No-Tracking queries are useful. They are very quick to execute because there is a change tracking setup. There are multiple ways to achieve no-tracking queries.

No-Tracking query using AsNoTracking() extention method ==>No Tracking can save both execution times and memory usage. Applying this option really becomes important when we retrieve a large amount of data from the database for read only purposes.