Client tracking
Applies To: # OData client v7 supported OData Client V7
OData Client for .NET supports two levels tracking : entity tracking and property tracking(only top level properties). Entity tracking enables you to track an entity in DataServiceContext
. You can enable property tracking by aid of DataServiceCollectionOfT
.
DataServiceContext
provides several ways to track an entity.
- Newly added entities will be automatically tracked.
- If you use
DataServiceContext.AttachTo
to attach an entity,DataServiceContext
will track the entity. - Entities returned by queries are also tracked if
DataServiceContext.MergeOption
is notMergeOption.NoTracking
.
Once entities are tracked, you can use DataServiceContext.EntityTracker
to get each entity descriptor which is used to describe the entity on client side. the entity tracker can also be used to get the link descriptor of all tracked links.
Once entities are tracked, the changes of these entities can be sent back to the data service when you call DataServiceContext.SaveChanges
method.
If you are using MergeOption.NoTracking
when you query an entity. You cannot get ETag of the entity from DataServiceContext
if it exists, since you cannot get the entity descriptor for the entity. Then, if you want to call AttachTo to track the entity, you need provide the ETag of the entity.
DataServiceContext
tracks each relationship as a link. You can use methods
AddRelatedObject
, AttachLink
, AddLink
, SetLink
, DetachLink
, DeleteLink
to track a link.
One sample to use AttachTo
and DeleteLink
.
DefaultContainer dsc = new DefaultContainer(new Uri("https://services.odata.org/V4/(S(uvf1y321yx031rnxmcbqmlxw))/TripPinServiceRW/"));
public void ClientEntityTracking()
{
var person = new Person()
{
UserName = "clydeguess"
};
var oneFriend = new Person()
{
UserName = "keithpinckney"
};
dsc.AttachTo("People", person);
dsc.AttachTo("People", oneFriend);
dsc.DeleteLink(person, "Friends", oneFriend);
dsc.SaveChanges();
}
Please refer to client property tracking for patch for detail.