Map key XML Schema (XSD) Constraints to DataSet Constraints

In a schema, you can specify a key constraint on an element or attribute using the key element. The element or attribute on which a key constraint is specified must have unique values in any schema instance, and cannot have null values.

The key constraint is similar to the unique constraint, except that the column on which a key constraint is defined cannot have null values.

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

Attribute name Description
msdata:ConstraintName If this attribute is specified, its value is used as the constraint name. Otherwise, the name attribute provides the value of the constraint name.
msdata:PrimaryKey If PrimaryKey="true" is present, the IsPrimaryKey constraint property is set to true, thus making it a primary key. The AllowDBNull column property is set to false, because primary keys cannot have null values.

In converting schema in which a key constraint is specified, the mapping process creates a unique constraint on the table with the AllowDBNull column property set to false for each column in the constraint. The IsPrimaryKey property of the unique constraint is also set to false unless you have specified msdata:PrimaryKey="true" on the key element. This is identical to a unique constraint in the schema in which PrimaryKey="true".

In the following schema example, the key element specifies the key constraint on the CustomerID element.

<xs:schema id="cod"  
  <xs:element name="Customers">  
        <xs:element name="CustomerID" type="xs:string" minOccurs="0" />  
        <xs:element name="CompanyName" type="xs:string" minOccurs="0" />  
       <xs:element name="Phone" type="xs:string" />  
<xs:element name="MyDataSet" msdata:IsDataSet="true">  
    <xs:choice maxOccurs="unbounded">  
      <xs:element ref="Customers" />  
   <xs:key  msdata:PrimaryKey="true"  
          name="KeyConstCustomerID" >  
     <xs:selector xpath=".//Customers" />  
     <xs:field xpath="CustomerID" />  

The key element specifies that the values of the CustomerID child element of the Customers element must have unique values and cannot have null values. In translating the XML Schema definition language (XSD) schema, the mapping process creates the following table:

Customers(CustomerID, CompanyName, Phone)  

The XML Schema mapping also creates a UniqueConstraint on the CustomerID column, as shown in the following DataSet. (For simplicity, only relevant properties are shown.)

      DataSetName: MyDataSet  
TableName: customers  
  ColumnName: CustomerID  
      AllowDBNull: False  
      Unique: True  
  ConstraintName: KeyCustID  
      Table: customers  
      Columns: CustomerID
      IsPrimaryKey: True  

In the DataSet that is generated, the IsPrimaryKey property of the UniqueConstraint is set to true because the schema specifies msdata:PrimaryKey="true" in the key element.

The value of the ConstraintName property of the UniqueConstraint in the DataSet is the value of the msdata:ConstraintName attribute specified in the key element in the schema.

See also