Chapter 21. Manipulating Entities with ObjectStateManager and MetadataWorkspace

It’s time to delve deeper into the Entity Framework and work directly with its core components: the ObjectStateManager and MetadataWorkspace APIs.

These are truly the two workhorses of the Entity Framework. Under the covers, Object Services uses the classes in these two APIs extensively to interact with objects at a granular level. Most of the classes and methods are public, so you can use ObjectStateManager and MetadataWorkspace in your own applications to control and manipulate entity objects.

Separately or together, these two classes not only allow you to manipulate entities, but also enable you to write generic methods that you can use on various Entity Framework object types, as well as dynamically create objects at runtime without depending on the generated entity classes.

Along with learning the concepts of ObjectStateManager and MetadataWorkspace, you will find many code samples in this chapter that you can use in your applications. For example, you will find a set of extension methods to overload the GetObjectStateEntries method and a utility for inspecting in-memory entities at runtime.

You will learn how to interact with entities and entity state through the ObjectStateManager and build the State Entry Visualizer that you saw in Chapter 10. Then you will learn about interacting with the raw metadata using the MetadataWorkspace.

Later in the chapter, you will build more samples that will give you ...

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.