Chapter 6. Modifying Entities and Saving Changes
So far, we have focused on the many ways to query an EDM to retrieve data from the database. This is only part of the Entity Framework story and the beginning of the life cycle of an entity. Once you have retrieved entities you can modify them, delete them, or even add new ones and then save all of these changes back to the database. In this chapter, we’ll take a high-level look at the way in which the Entity Framework is able to track these changes and get the necessary data back to the database. Then we’ll watch updates, inserts, and deletions in action, not only in code samples, but also in terms of what happens in the database in response.
Later chapters will focus on modifying the default behavior.
Keeping Track of Entities
In the previous chapters, you used an ObjectContext
, the SampleEntities
class (renamed “PEF” in Chapter 5), which inherits from
ObjectContext
, to create and execute
queries. You also worked with the objects that were returned by those
queries, whether they were entities, anonymous types, or objects within
a DbDataRecord
. The nature of this
interaction was to iterate through the objects and extract a few
properties to display in a console window.
The context can also keep track of these entities once they’ve been returned by a query. As your application logic modifies the objects, the context is notified and makes note of changes. The context is responsible for managing the state of its entities, including those ...
Get Programming Entity Framework, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.