Chapter 5. Modifying Entities and Saving Changes

So far, we have focused on many ways to retrieve data from the database through the Entity Data Model (EDM). 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 default way in which the Entity Framework tracks these changes and performs updates. This will prepare you for most of what you will be learning throughout the rest of the book, whether you are using high-level data-binding features or working deeply under the covers.

Note

In the next chapter, you’ll learn about overriding the default behavior for both querying and updating by using stored procedures.

How ObjectContext Manages Entities

In Chapters 3 and 4, you used an ObjectContext, the ProgrammingEFDB1Entities class, 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 the console windows.

Those objects were created by an internal process called object materialization, which takes the returned data and builds the relevant objects for you. Depending on the query, these could ...

Get Programming Entity Framework 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.