In a lot of cases, data retrieved by the Entity Framework will not be modified within the scope of the same DBContext. Typical examples of this are ASP.NET MVC or ASP.NET MVC API action methods that just answer a get request. However, if you're using the default way of retrieving data, Entity Framework will prepare everything to be able to detect changes on your retrieved entities in order to persist those changes later in the database. This doesn’t only add a performance penalty, but costs some memory, too.
A typical method of retrieving data is:
var products = db.Products.Where(p => p.InStock).ToList();
A better way uses the extension method AsNoTracking from the System.Data.Entity Namespace:
var products = db.Products.Where(p => p.InStock).AsNoTracking().ToList();
No comments :
Post a Comment