Sunday, October 2, 2011

Entity Relationship in MS CRM 4.0

A 1:N (one-to-many) Relationship is a hierarchical relationship created or viewed from the primary entity. Any one entity instance from the primary entity can be referenced by many entity instances from the related entity.

An N:1 (many-to-one) Relationship is a hierarchical relationship created or viewed from the related entity. Many entity instances from the related entity can reference any one entity instance from the primary entity.

Remember that the same relationship can be viewed from either of the two entities that participate in the relationship.

Both relationships are represented by the OneToManyMetadata class. Notice that the Microsoft Dynamics CRM Web application uses the terms primary entity and related entity while the metadata service uses the terms referenced entity and referencing entity.

N:N (many-to-many) Relationships
A many-to-many relationship lets users relate one or more entity instances from another entity to an entity instance of the current entity. A many-to-many relationship is reciprocal. Therefore, entity instances can be related from either entity. A many-to-many relationship may also be self-referential. This means that one or more other entity instances of the current entity can be related to an entity instance of the same entity.

This kind of relationship is represented by the ManyToManyMetadata class.

When you create a many-to-many relationship, an intersect table is created. You can use the intersect tables in the QueryExpression for a RetrieveMultiple query, but you cannot retrieve the intersect table records directly with the RetrieveMultiple method. To retrieve the records in an intersect table, you must use the Fetch method. For more information, see Using Intersect Tables.

Self-Referential Relationships
In a self-referential relationship, an entity has a relationship with itself. Both hierarchical relationships and many-to-many relationships can be self-referential. This allows entity instances to be directly associated with other entity instances of the same type. For example, opportunities can be linked to related opportunities.

The only limitation to self-referential relationships is that entity instances cannot be related to themselves in a parental relationship. This creates a circular reference.