Udostępnij za pośrednictwem


ReferentialConstraint Element (CSDL)

A ReferentialConstraint element in conceptual schema definition language (CSDL) defines functionality that 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.

If a foreign key that is exposed on one entity type references a property on another entity type, the ReferentialConstraint element defines an association between the two entity types. Because the ReferentialConstraint element provides information about how two entity types are related, no corresponding AssociationSetMapping element is necessary in the mapping specification language (MSL). An association between two entity types that do not have foreign keys exposed must have a corresponding AssociationSetMapping element in order to map association information to the data source.

If a foreign key is not exposed on an entity type, the ReferentialConstraint element can only define a primary key-to-primary key constraint between the entity type and another entity type.

A ReferentialConstraint element can have the following child elements (in the order listed):

Applicable Attributes

The ReferentialConstraint element can have any number of annotation attributes (custom XML attributes). However, custom attributes may not belong to any XML namespace that is reserved for CSDL. The fully-qualified names for any two custom attributes cannot be the same.

Example

The following example shows a ReferentialConstraint element being used as part of the definition of the PublishedBy association.

<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 Framework Overview
CSDL Specification
Schema Element (CSDL)
Association Element (CSDL)

Other Resources

CSDL, SSDL, and MSL Specifications
ADO.NET Entity Data Model Tools
referential integrity constraint (Entity Data Model)