The objectcontext API

Image

Until the release of version 4.1, the ObjectContext API was the only game in town, and it’s still the API you get by default when you use the Entity Model Designer (despite the fact that Microsoft recommends the DbContext API for new development). So let’s start with it:

Image

Image Put On Your Thinking Hat

Using the types shown in the basic pattern diagram, which type of object do you think would be used to perform the following tasks?

 

Add functionality to a specific type, like the ability for a Recipe to calculate nutritional values, for example?

 

 

Retrieve the name of the database being used as the data store?

 

 

Determine whether data has changed since it was retrieved from a database?

 

 

Retrieve objects that meet specific criteria from the data store? (Entity Framework calls this MATERIALIZATION.)

 

 

Change the value of a specific property?

 

 

Persist changes to the data store?

 



Image Put On Your Thinking Hat

How’d you do?

 

Add functionality to a specific type, like the ability for a Recipe to calculate nutritional values, for example?

To add functionality to a type of entity, you would extend the class that represents the concrete entity object. (The Recipe class in our example.)

Retrieve the name of the database being used as the data store?

You can get this information from the object that represents the connection: DbConnection.

Determine whether data has changed since it was retrieved from a database?

You would query the ObjectStateManager.

Retrieve objects that meet specific criteria from the data store? (Entity Framework calls this MATERIALIZATION.)

You’ll use the ObjectContext object for this, and we’ll see how in Chapter 8.

Change the value of a specific property?

You’d change the property of the entity instance, and the Entity Framework will handle all the change tracking.

Persist changes to the data store?

Just call the SaveChanges() method of the ObjectContext class. The Entity Framework will handles all the updates, additions and changes, and even handle fixing up identity values in new entities and their relationships.

 

 


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset