Share via


Mapping keyref XML Schema (XSD) Constraints to DataSet Constraints 

The keyref element allows you to establish links between elements within a document. This is similar to a foreign key relationship in a relational database. If a schema specifies the keyref element, the element is converted during the schema mapping process to a corresponding foreign key constraint on the columns in the tables of the DataSet. By default, the keyref element also generates a relation, with the ParentTable, ChildTable, ParentColumn, and ChildColumn properties specified on the relation.

The following table outlines the msdata attributes you can specify in the keyref element.

Attribute name Description

msdata:ConstraintOnly

If ConstraintOnly="true" is specified on the keyref element in the schema, a constraint is created, but no relation is created. If this attribute is not specified (or is set to False), both the constraint and the relation are created in the DataSet.

msdata:ConstraintName

If the ConstraintName attribute is specified, its value is used as the name of the constraint. Otherwise, the name attribute of the keyref element in the schema provides the constraint name in the DataSet.

msdata:UpdateRule

If the UpdateRule attribute is specified in the keyref element in the schema, its value is assigned to the UpdateRule constraint property in the DataSet. Otherwise the UpdateRule property is set to Cascade.

msdata:DeleteRule

If the DeleteRule attribute is specified in the keyref element in the schema, its value is assigned to the DeleteRule constraint property in the DataSet. Otherwise the DeleteRule property is set to Cascade.

msdata:AcceptRejectRule

If the AcceptRejectRule attribute is specified in the keyref element in the schema, its value is assigned to the AcceptRejectRule constraint property in the DataSet. Otherwise the AcceptRejectRule property is set to None.

The following example contains a schema that specifies the key and keyref relationships between the OrderNumber child element of the Order element and the OrderNo child element of the OrderDetail element.

In the example, the OrderNumber child element of the OrderDetail element refers to the OrderNo key child element of the Order element.

<xs:schema id="MyDataSet"  
            xmlns:xs="https://www.w3.org/2001/XMLSchema" 
            xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

 <xs:element name="MyDataSet" msdata:IsDataSet="true">
  <xs:complexType>
    <xs:choice maxOccurs="unbounded">
      <xs:element name="OrderDetail">
       <xs:complexType>
         <xs:sequence>
           <xs:element name="OrderNo" type="xs:integer" />
           <xs:element name="ItemNo" type="xs:string" />
         </xs:sequence>
       </xs:complexType>
      </xs:element>
      <xs:element name="Order">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="OrderNumber" type="xs:integer" />
            <xs:element name="EmpNumber" type="xs:integer" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:choice>
  </xs:complexType>

  <xs:key name="OrderNumberKey"  >
    <xs:selector xpath=".//Order" />
    <xs:field xpath="OrderNumber" />
  </xs:key>

  <xs:keyref name="OrderNoRef" refer="OrderNumberKey">
    <xs:selector xpath=".//OrderDetail" />
    <xs:field xpath="OrderNo" />
  </xs:keyref>
 </xs:element>
</xs:schema>

The XML Schema definition language (XSD) schema mapping process produces the following DataSet with two tables:

OrderDetail(OrderNo, ItemNo) and
Order(OrderNumber, EmpNumber)

In addition, the DataSet defines the following constraints:

  • A unique constraint on the Order table.

              Table: Order
    Columns: OrderNumber 
    ConstraintName: OrderNumberKey
    Type: UniqueConstraint
    IsPrimaryKey: False
    
  • A relationship between the Order and OrderDetail tables. The Nested property is set to False because the two elements are not nested in the schema.

              ParentTable: Order
    ParentColumns: OrderNumber 
    ChildTable: OrderDetail
    ChildColumns: OrderNo 
    ParentKeyConstraint: OrderNumberKey
    ChildKeyConstraint: OrderNoRef
    RelationName: OrderNoRef
    Nested: False
    
  • A foreign key constraint on the OrderDetail table.

              ConstraintName: OrderNoRef
    Type: ForeignKeyConstraint
    Table: OrderDetail
    Columns: OrderNo 
    RelatedTable: Order
    RelatedColumns: OrderNumber 
    

See Also

Concepts

Generating DataSet Relations from XML Schema (XSD)

Other Resources

Mapping XML Schema (XSD) Constraints to DataSet Constraints