Aug
29
2009

Getting to know the EntityCollection

Meet the EntityCollection

Other than the DomainClient, the EntityCollection is probably the least known part of RIA Services. This is a problem as the EntityCollection is probably the most used class other than the Entity itself. The primary job of the EnityCollection is to represent one to many relationships between entities. It does this in two different ways.

The Detached EntityCollection

When your entity is detached, meaning it isn’t tracked by an EntityList, the EntityCollection acts as a container of related entities. You can create an entire hierarchy of objects and it will all be held together by the EntityCollections. When you finally add your entity to an EntityList all of the related entities from the EntityCollections are also added to their respective EntityLists. This begins the EntityCollection’s other mode.

The Attached EntityCollection

For entities tracked by EntityLists, the EntityCollection takes on some of the attributes of a CollectionView where the EntityList is now the underlying container of entities. The EntityCollection listens for changes in the EntityList and uses a filter to determine what entities should be viewed, much like a CollectionView. However, unlike a CollectionView the EntityCollection does not have a Refresh method. This means that the filter method has to be static, only changes to the EntityList will change the contents of the EntityCollection.

Adding and Removing objects through the EntityCollection

The EntityCollection has Add and Remove methods and while dealing with detached entities these methods work fine. However, using those methods with attached entities can be confusing. The EntityCollection’s add and remove do not actually delete the entity, it just clears the values defined in the AssociationAttribute. This means that you have set the ParentID in the child table to Null or 0. This is probably not what you meant to do. In general, I would suggest not using the Add and Remove method of the EntityCollection when dealing with attached entities. Instead, deal directly with the EntityLists instead.

Continue to Advanced EntityCollection

blog comments powered by Disqus

Month List