Here are some of the ODATA context properties that would help us during debugging and understanding the behavior of the context when performing various operations.
1. DataServiceContext.Entities:
Gets a list of all the records currently being tracked by the DataServiceContext. While using OData service, we frequently get the error that context already tracking the record. With the “Entities” property, we can check total records tracked by context. As well as after adding record to the set, we can check whether context.Entities count increases.
2. DataServiceContext.IgnoreMissingProperties:
Gets or sets whether the properties read from the type must be mapped to properties on the client-side type. Always set this property to true. This helps when say you have downloaded the ODATA reference and developed your application based on that and later there are additional attributes added to the entities. With this property set to True it will ignore the new properties that were added later on and not throw an error regarding missing properties.
3. DataServiceContext.Links:
Gets the collection of all associations or links currently being tracked by the DataServiceContext object which are added using AddRelatedObject, AddLink and SetLink methods.
4. DataServiceContext.AddLink(source, sourceProperty, target):
Adds the specified link to the set of objects the DataServiceContext is tracking. Before adding a link, both source and target must be tracked by context.
Example: context.AddLink(parentRecord, “relationshipName”, childRecord);
5. DataServiceContext.SetLink(source, sourceProperty, target):
Notifies the DataServiceContext that a new link exists between the objects specified and that the link is represented by the property specified by the sourceProperty parameter. Before adding a link, both source and target must be tracked by context.
Example: context.AddLink(childRecord, “relationshipName”, parentRecord);
6. DataServiceContext.AddRelatedObject(source, sourceProperty, target):
Adds a related object to the context and creates the link that defines the relationship between the two objects in a single request.
Example: context.AddLink(childRecord, “relationshipName”, parentRecord);
Note: AddLink and SetLink are very useful methods as using these we can make a compound request (i.e. can add parent and child together). This will only work when your parentLink (a field that is lookup to parent) is not required on child record. When we tried to create an invoice with invoice details, it was giving an error and only invoice header gets created. This is because for Invoice detail, invoiceid is a required field and it cannot be created without and invoice id provided. However this does work in cases, where the child entity can be created independently and the parent entity is not a required attribute in the child entity.
——————————————————-
Posted by: Inogic
For more information/discussions (documents, sample code snippets, detailed work flow or diagrams),
Please be free to visit the following links or email us:
Web: http://www.inogic.com
Blog: http://inogic.blogspot.com
Email: news@inogic.com
—————————————————–