Managing Relationships between Persistence-Ignorant Objects (Entity Framework)
Relationships between objects are represented by navigation properties, where each relationship between two objects is tracked by an instance of the RelationshipManager class and by a special ObjectStateEntry in the ObjectStateManager. When a data class implements the required interfaces, as is the case when proxy object creation is enabled and with classes that inherit from EntityObject, these relationships are managed automatically by Object Services. If you cannot meet the additional requirements in your custom classes, then proxy object creation is automatically disabled. You can also disable proxy class generation if you do not want these proxy objects to be materialized with your objects. For more information, see Persistence-Ignorant Objects (Entity Framework). When the creation of proxy objects is disabled or when a persistence-ignorant object is created without its proxy object, you must manually maintain relationships between objects.
Manually Adding, Deleting, and Changing Related Objects
A navigation property may return a single related object reference, as is the case in a one-to-one or a many-to-one relationship, or a collection of related objects, as is the case in a one-to-many or a many-to-many relationship. A new relationship is created between two persistence-ignorant objects when the navigation property of one object is set to reference a newly created related object (one-to-one or many-to-one) or when a newly created object is added to a collection of related objects (one-to-many or many-to-many). In the same way, a relationship is deleted when an object is deleted from a collection of related objects, when a navigation property that referenced an existing object is set to null, or when there is a constrained relationship and a deleted object requires related objects to be deleted.
Note |
|---|
|
When a referential constraint exists between two objects, the dependent object is deleted when it is removed from the collection of related objects. |
When proxy generation is disabled, changes to the navigation properties, which represent these relationship changes, cannot be detected by Object Services until after DetectChanges is called. For more information, see Tracking Changes in Persistence-Ignorant Objects (Entity Framework).
Managing Bi-Directional Relationships
While not explicitly required by a data model, most of the time relationships are bi-directional. This means that a one-to-many relationship represented by the navigation property of one object has the opposite many-to-one relationship exposed as a navigation property on the related object. For example, if an Order object has a navigation property that returns related LineItem objects, and then each LineItem has a navigation property that returns a reference to an Order. When proxy object creation is disabled, Object Services cannot automatically set the opposite end of a bi-directional relationship when one end changes. This means that you must ensure that the opposite ends of bi-directional relationship is set when one end is set. For example, if you add a new LineItem object to a collection returned by the navigation property of an Order object, you must also ensure that the Order object is assigned to the navigation property on the LineItem that references the Order. For more information, see How to: Change Relationships Between Persistence-Ignorant Objects (Entity Framework).
Bi-directional relationships between objects are always set automatically for objects returned by a query or a load operation. For more information, see Shaping Query Results (Entity Framework).
Note