Referential Constraints (Entity Framework)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

In an Entity Data Model (EDM), referential constraints can exist between related objects, such as between the SalesOrderHeader and SalesOrderDetail objects. These constraints are specified in the conceptual schema definition language (CSDL) file. For more information, see ReferentialConstraint Element (Association CSDL).


Object Services does not enforce all constraints that can be expressed in an EDM. For example, Object Services does not enforce cardinality constraints on relationships, and this can cause inconsistent results when you save changes to related objects to the data source. Ensure that all critical model constraints are enforced by the data source.

When you work with objects that have constrained relationships, be aware of the following points:

  • Dependent (child) objects cannot exist without a defined relationship to the parent object.

  • When you create a new child object, the parent object must exist in the object context or in the data source before SaveChanges is called. Otherwise, an InvalidOperationException will be raised.

  • Use the Add method to add child objects to the EntityCollection of the parent object. The collection of child objects is accessed from a navigation property that has been defined for the relationship, or from the GetRelatedEnd or GetRelatedCollection methods on the RelationshipManager property of the parent object.

  • Deleting the parent object also deletes all the child objects in the constrained relationship. This result is the same as enabling the CascadeDelete property on the association for the relationship.

  • Removing the relationship deletes the child object. This means that calling Remove on the EntityCollection marks both the relationship and the child object for deletion.

  • After a relationship has been created, it can only be changed by the following process:

    1. Drop the existing relationship.

    2. Call SaveChanges.

    3. Add the persisted child object to the EntityCollection of the new parent object.

See Also

Community Additions