Friday, April 3, 2020

Tracking queries in Entity Framework Core

Entities retrieved using Entity Framework Core LINQ queries have tracking enabled by default, this allows us to make changes to the entity and save them back to the database using SaveChanges. When we execute a query in Entity Framework Core the resultant object is stored in a cache which allows us to make changes and use the object to update the database.
            var employee = _context.Employee.Where(e => e.EmployeeId == 1)
                            .FirstOrDefault<Employee>();

            employee.Age = 38;
            employee.City = "Chicago";
            employee.State = "IL";

            _context.SaveChanges();

In the above code snippet we get the Employee entity object in the first line, we update the properties of the entity and same the changes in the last line using SaveChanges. Since tracking is enabled by default the changes to the object are stored in the cache and updated to the database.  


Search Flipkart Products:
Flipkart.com

No comments: