Udostępnij za pośrednictwem


Object Services Overview (Entity Framework)

Object Services is a component of the Entity Framework that enables you to query, insert, update, and delete data, expressed as strongly typed CLR objects that are instances of entity types. Object Services supports both Language-Integrated Query (LINQ) and Entity SQL queries against types that are defined in the conceptual model. Object Services materializes returned data as objects, and propagates object changes back to the data source. It also provides facilities for tracking changes, binding objects to controls, and handling concurrency. Object Services is implemented by classes in the System.Data.Objects and System.Data.Objects.DataClasses namespaces.

Object Context

The ObjectContext class is the primary class for interacting with data in the form of objects that are instances of entity types that are defined in a conceptual model. An instance of the ObjectContext class encapsulates the following:

The Entity Framework tools consume a conceptual schema definition language (CSDL) file and generate the object-layer code. This code is used to work with entity data as objects and to take advantage of Object Services functionality. This generated code includes the following data classes:

  • A typed ObjectContext class. This class represents the EntityContainer for the model and is derived from ObjectContext.

  • Classes that represent entity types and inherit from EntityObject.

  • Classes that represent complex types and inherit from ComplexObject.

Note

The ObjectContext class is not thread safe. The integrity of data objects in an ObjectContext cannot be ensured in multi-threaded scenarios.

Using Object Services

Object Services supports the following behavior for programming against the Entity Framework.

Querying data as objects

Object Services enables you to use LINQ, Entity SQL, or query builder methods to execute queries against a conceptual model and return data as objects. For more information, see Object Queries (Entity Framework).

Shaping query results

By default, Object Services only returns objects specifically requested in the query. When relationships exist between objects, you can specify whether a query returns related objects. You can also load related objects in a later request. For more information, see Loading Related Objects (Entity Framework).

Composing queries using builder methods

Object Services provides methods on ObjectQuery that are used to construct queries that are equivalent to Entity SQL and LINQ to Entities queries. For more information, see Query Builder Methods (Entity Framework).

Adding, changing, and deleting objects

Object Services persists data objects in memory and enables you to add, modify, and delete objects within an object context. Changes made to objects are tracked by the object context. For more information, see Creating, Adding, Modifying, and Deleting Objects (Entity Framework).

Saving changes to the data source

Object Services caches changes to objects in the object context. When explicitly requested, Object Services saves those changes back to the data source. For more information, see Saving Changes and Managing Concurrency (Entity Framework).

Binding objects to controls

Objects Services enables the binding of objects to controls that support data binding, such as the DataGridView control. For more information, see Binding Objects to Controls (Entity Framework).

Attaching objects

Objects Services enables you to attach existing objects directly to an object context. This enables you to attach objects that have been stored in the view state of an ASP.NET application or have been returned from a remote method call or Web service. For more information, see Attaching and Detaching Objects (Entity Framework).

Detaching objects

An object context instance might need to be persisted for the duration of application execution, such as when objects are bound to Windows Form controls. Object Services enables you to manage the size of the object context by detaching objects to release resources when they are no longer needed. For more information, see Attaching and Detaching Objects (Entity Framework).

Serializing objects

Object Services supports Windows Communication Foundation (WCF) data contract serialization, binary serialization, and XML serialization for objects. Data contract serialization is useful in Web services scenarios. Binary serialization is especially useful when using View State to persist objects in an ASP.NET application. For more information, see Serializing Objects (Entity Framework).

Managing object identities and tracking changes

Object Services uses identity values to track changes to objects, handle conflicts, and decide when to retrieved data from the data source. For more information, see Identity Resolution, State Management, and Change Tracking (Entity Framework).

Managing concurrency

Object Services can track concurrency when the ConcurrencyMode attribute for one or more properties is set to "fixed." In this case, Object Services will raise specific exceptions when concurrency violations are detected. For more information, see Saving Changes and Managing Concurrency (Entity Framework).

Managing connections

Object Services enables you to explicitly manage the connection used by an object context and provide your own connection for the object context. For more information, see Managing Connections and Transactions (Entity Framework).

Managing transactions

Object Services supports .NET Framework transactions to coordinate operations against the data source and to enlist in distributed transactions. For more information, see Managing Connections and Transactions (Entity Framework).

Using custom objects with a Conceptual Model

Object Services enables you to manually define your own objects or use existing objects with a conceptual model. For more information, see Customizing Objects (Entity Framework).

See Also

Concepts

Working with Objects (Entity Framework)
Entity Framework Overview