Chapter 17. Controlling Objects 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.
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 also take advantage of them to control and manipulate entity objects at a low level.
In this chapter, you will build some pretty intensive code samples
that will give you great hands-on experience working at this level. You
will also get some ideas of what you can achieve with ObjectStateManager
and MetadataWorkspace
. In fact, most of the object
interaction under the covers occurs using ObjectStateEntries
and MetadataWorkspace
. You have direct access
to the same capabilities.
Combined, these two classes not only allow you to manipulate objects, 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.
There are many benefits to writing generic code for entity objects.
It allows you to write reusable code that can handle any EntityObject
, whether processing entities in
SavingChanges
,
writing utilities, or even, as you’ll see in this chapter, writing code
that can dynamically create entities.
Managing ObjectStateEntry Objects with ObjectStateManager
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.