referential integrity constraint

A referential integrity constraint in the Entity Data Model (EDM) is similar to a referential integrity constraint in a relational database. In the same way that a column (or columns) from a database table can reference the primary key of another table, a property (or properties) of an entity type can reference the entity key of another entity type. The entity type that is referenced is called the principal end of the constraint. The entity type that references the principal end is called the dependent end of the constraint.

A referential integrity constraint is defined as part of an association between two entity types. The definition for a referential integrity constraint specifies the following information:

  • The principal end of the constraint. (An entity type whose entity key is referenced by the dependent end.)

  • The entity key of the principal end.

  • The dependent end of the constraint. (An entity type that has a property or properties that reference the entity key of the principal end.)

  • The referencing property or properties of the dependent end.

The purpose of referential integrity constraints in the EDM is to ensure that valid associations always exist. For more information, see foreign key property.

Example

The diagram below shows a conceptual model with two associations: WrittenBy and PublishedBy. The Book entity type has a property, PublisherId, that references the entity key of the Publisher entity type when you define a referential integrity constraint on the PublishedBy association.

RefConstraintModel

The ADO.NET Entity Framework uses a domain-specific language (DSL) called conceptual schema definition language (CSDL) to define conceptual models. The following CSDL defines a referential integrity constraint on the PublishedBy association shown in the conceptual model above.

<Association Name="PublishedBy">
  <End Type="BooksModel.Book" Role="Book" Multiplicity="*" >
  </End>
  <End Type="BooksModel.Publisher" Role="Publisher" Multiplicity="1" />
  <ReferentialConstraint>
    <Principal Role="Publisher">
      <PropertyRef Name="Id" />
    </Principal>
    <Dependent Role="Book">
      <PropertyRef Name="PublisherId" />
    </Dependent>
  </ReferentialConstraint>
</Association>

See also