Udostępnij za pośrednictwem


referential integrity constraint (Entity Data Model)

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 (Entity Data Model).

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.

Ee382827.RefConstraintModel(en-us,VS.100).gif

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

Concepts

Entity Data Model Key Concepts

Entity Data Model