This section briefly introducesObjectcontext, Objectquery,Objectstateentry,ObjectstatemanagerThese important classes are all located under the system. data. Data. entity. dll namespace system. Data. Objects. In subsequent chapters, we often use some of their methods to accomplish some of our operations or purposes. In this section, we will briefly describe the methods of various classes that we may use in the future to facilitate our subsequent study.
ObjectcontextEncapsulate connections between. NET Framework and databases. This type is used as the gateway for "CREATE", "read", "Update", and "delete" operations.
Objectcontext Class is the primary class used to interact with data of objects (instances of the entity type defined in EDM.
ObjectcontextAn instance of the class encapsulates the following content:
LThe connection to the database is encapsulated as an entityconnection object.
LDescribes the metadata of the model, which is encapsulated as a metadataworkspace object.
LUsed to manage the persistent storage objects in the cacheObjectstatemanager Object.
ObjectcontextClass member methods are described as follows:
LAcceptallchanges ()
Accept all changes to this object
LAddobject (string, object)
Add an object to a specified object container
LApplypropertychanges (string, object)
Apply the changes to the attributes of the specified object to the original object in the container.
LAttach (system. Data. Objects. dataclasses. ientitywithkey entity)
Attaches an object with a primary key to the default container.
LAttach (string, object)
Attaches an object to a specified object container.
LCreateentitykey (string, object)
Create an object primary key for a specified object or directly return the primary key of the object if an object primary key already exists.
LCreatequery <t> (string, Params objectparameter [])
Creates an objectquery object from a given query string.
LDeleteobject (object)
Deletes a specified object.
LDetach (object)
Removes a specified object.
LExecutefunction <telement> (string, Params objectparameter [])
Execute the given function for the default container.
LGetobjectbykey (system. Data. entitykey key)
FromObjectstatemanager Object (if any); otherwise, it is retrieved from the bucket.
LRefresh (system. Data. Objects. refreshmode, object entity)
Update Data in the storage area of the specified object in the specified persistent update modeObjectstatemanager..
LRefresh (system. Data. Objects. refreshmode, system. Collections. ienumerable collection)
Update Data in the storage area of the specified object set in the specified persistent processing modeObjectstatemanager.
LSavechanges (bool)
Save all updates permanently to the bucket. Parameters are required for client transaction support. If the parameter is true, the changes are automatically appliedObjectstatemanager. If the value is false, you need to callAcceptallchanges() For updatesObjectstatemanager.
LSavechanges ()
Save all updates to the bucket permanently
LTrygetobjectbykey (system. Data. entitykey, out object)
Attempts to return the object from the specified Primary Key
The specific usage of the above methods will be described later.
Next, let's look at the useful class objectquery.
ObjectqueryThere is a useful methodTotracestring ()This method is used to track the executed SQL statements. Through this method, we can obtain the executed SQL statements so that we can view and analyze the specific executed SQL statements. (Similar to the showsql section in the nhib.pdf configuration file)
Learn moreObjectstateentry.
ObjectstateentryMaintains the status (added, deleted, separated, modified, or not changed), key value, and original value of an entity or relational instance. You can also manage the list of attributes that have been modified. It includes the following methods:
LAcceptchanges
Accept the current value as the original value and mark the object as unchanged ().
LDelete
Mark the object as deleted (). If the entity is in the added () state, it will be detached ().
LGetmodifiedproperties
Returns the name of the Property marked as modified.
LSetmodified
Set the status to modified ().
LSetmodifiedproperty
Mark the specified attribute as modified ().
Next, let's take a look.Objectstatemanager.
ObjectstatemanagerIt is used to maintain the persistence of object ing, object status/ID management, and entity or relational instances.
LGetobjectstateentries
ObtainObjectstateentrySet.
LGetobjectstateentry
Obtains the correspondingObjectstateentry
Now, several important classes have been briefly introduced. Later, we will learn how to use them.