Introduction to the "Entity Framework"--objectcontext, ObjectQuery, ObjectStateEntry, Objectstatemanager classes

Source: Internet
Author: User

This section briefly introduces ObjectContext, ObjectQuery, ObjectStateEntry, Objectstatemanager, which are some of the more important classes in EF, They are located under the System.Data.Objects namespace under System.Data.Entity.dll. In subsequent chapters, we often use some of their methods in order to accomplish some of our operations or purposes. In this section, we will briefly explain the various classes of methods we may use in the future to facilitate our subsequent learning.

ObjectContext encapsulates the connection between the. NET Framework and the database. This class is used as a gateway for Create, read, update, and delete operations.

The ObjectContext class is used as the primary class for interacting with data as objects that are instances of the entity types defined in the EDM.

An instance of the ObjectContext class encapsulates the following:

A connection to the database, encapsulated in the form of a EntityConnection object.

L describes the metadata of the model, encapsulated in the form of an MetadataWorkspace object.

L Objectstatemanager object for managing persisted objects in the cache.

The member methods of the ObjectContext class are illustrated as follows:

L AcceptAllChanges ()

Accept all changes to the entity object

L AddObject (String,object)

To add an entity object to a developed entity container

L Applypropertychanges (String,object)

The changes to the assigned entity object properties are applied to the corresponding original object in the container.

L Attach (System.Data.Objects.DataClasses.IEntityWithKey entity)

Attaching an Entity object with a primary key to the default container

L Attach (String,object)

Attaching an Entity object to a specified entity container

L Createentitykey (String,object)

Creates an entity primary key for the specified entity object or returns the primary key of the entity if the entity primary key already exists

L createquery<t> (string,params objectparameter[])

Creates a ObjectQuery object from the given query string.

L DeleteObject (object)

Deletes the specified Entity object

L Detach (object)

Removes the specified Entity object

L executefunction<telement> (string,params objectparameter[])

Executes the given function on the default container.

L Getobjectbykey (System.Data.EntityKey key)

Retrieves an object from the Objectstatemanager, if it exists, from the primary key, otherwise it is retrieved from the store.

L Refresh (System.Data.Objects.RefreshMode RefreshMode, object entity)

Updates Objectstatemanager by specifying the persistent update mode, using the specified entity's store data:

L Refresh (System.Data.Objects.RefreshMode RefreshMode, System.Collections.IEnumerable Collection)

Updates the Objectstatemanager with the storage data for the specified entity set, in the specified persistent processing mode.

L SaveChanges (BOOL)

Persist all updates to the store. Parameters are parameters required for client transaction support. True to automatically apply changes to the entities in the Objectstatemanager after the update. If False, you will also need to call AcceptAllChanges () after the update to update the entities in the Objectstatemanager.

L SaveChanges ()

Persist all updates to the store

L Trygetobjectbykey (System.data.entitykey,out object)

Attempt to return the entity from the specified entity primary key

The specific usage of each of these methods will be described later.

Next, look at the useful class ObjectQuery.

ObjectQuery has a useful method of totracestring (), which is used to trace the SQL statements executed, which allows us to get the SQL statements executed so that we can view and parse the SQL statements that are executed. (similar to the Showsql section in the NHibernate configuration file)

Find out more about ObjectStateEntry.

ObjectStateEntry maintains the state of an entity instance or relationship instance (added, deleted, detached, modified, or unchanged), key values, and original values. Also manages the list of properties that have been modified. It contains the following methods:

L AcceptChanges

Accepts the current value as the original value and marks the entity as unchanged ().

L Delete

Marks the entity as Deleted (). If the entity is in the Added () () () () state, it will be Detached ().

L Getmodifiedproperties

Returns the name of the property that is marked Modified ().

L SetModified

Set the status to Modified ().

L Setmodifiedproperty

Marks the specified property as Modified ().

Then, look at Objectstatemanager.

Objectstatemanager is used to maintain object mappings, object state/Identity management, and persistence of entity instances or relationship instances.

L Getobjectstateentries

Gets the ObjectStateEntry collection for the given entitystate.

L Getobjectstateentry

Gets the corresponding objectstateentry for the given EntityKey

Now, a few important classes are simply introduced. Later, we will specifically learn about their use.

Introduction to the "Entity Framework"--objectcontext, ObjectQuery, ObjectStateEntry, Objectstatemanager classes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.