Export (0) Print
Expand All

External Mapping Reference (LINQ to SQL)

LINQ to SQL supports external mapping, a process by which you use a separate XML file to specify mapping between the data model of the database and your object model. Advantages of using an external mapping file include the following:

  • You can keep your mapping code out of your application code. This approach reduces clutter in your application code.

  • You can treat an external mapping file something like a configuration file. For example, you can update how your application behaves after shipping the binaries by just swapping out the external mapping file.

The mapping file must be an XML file, and the file must validate against a LINQ to SQL schema definition (.xsd) file. 

The following rules apply:

  • The mapping file must be an XML file.

  • The XML mapping file must be valid against the XML schema definition file. For more information, see How to: Validate DBML and External Mapping Files (LINQ to SQL).

  • External mapping overrides attribute-based mapping. In other words, when you use an external mapping source to create a DataContext, the DataContext ignores all mapping attributes you have created on classes. This behavior is true whether the class is included in the external mapping file.

  • LINQ to SQL does not support the hybrid use of the two mapping approaches (attribute-based and external).

External mapping in LINQ to SQL must be valid against the following XML schema definition.

Distinguish this schema definition file from the schema definition file that is used to validate a DBML file. For more information, see Code Generation in LINQ to SQL).

NoteNote:

Visual Studio users will also find this XSD file in the XML Schemas dialog box as "LinqToSqlMapping.xsd". To use this file correctly for validating an external mapping file, see How to: Validate DBML and External Mapping Files (LINQ to SQL).

?<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.microsoft.com/linqtosql/mapping/2007" xmlns="http://schemas.microsoft.com/linqtosql/mapping/2007"
elementFormDefault="qualified" >
  <xs:element name="Database" type="Database" />
  <xs:complexType name="Database">
    <xs:sequence>
      <xs:element name="Table" type="Table" minOccurs="0" maxOccurs="unbounded" />
      <xs:element name="Function" type="Function" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="optional" />
    <xs:attribute name="Provider" type="xs:string" use="optional" />
  </xs:complexType>
  <xs:complexType name="Table">
    <xs:sequence>
      <xs:element name="Type" type="Type" minOccurs="1" maxOccurs="1" />
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="optional" />
    <xs:attribute name="Member" type="xs:string" use="optional" />
  </xs:complexType>
  <xs:complexType name="Type">
    <xs:sequence>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Column" type="Column" minOccurs="0" maxOccurs="unbounded" />
        <xs:element name="Association" type="Association" minOccurs="0" maxOccurs="unbounded" />
      </xs:choice>
      <xs:element name="Type" type="Type" minOccurs="0" maxOccurs="unbounded" />
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="required" />
    <xs:attribute name="InheritanceCode" type="xs:string" use="optional" />
    <xs:attribute name="IsInheritanceDefault" type="xs:boolean" use="optional" />
  </xs:complexType>
  <xs:complexType name="Column">
    <xs:attribute name="Name" type="xs:string" use="optional" />
    <xs:attribute name="Member" type="xs:string" use="required" />
    <xs:attribute name="Storage" type="xs:string" use="optional" />
    <xs:attribute name="DbType" type="xs:string" use="optional" />
    <xs:attribute name="IsPrimaryKey" type="xs:boolean" use="optional" />
    <xs:attribute name="IsDbGenerated" type="xs:boolean" use="optional" />
    <xs:attribute name="CanBeNull" type="xs:boolean" use="optional" />
    <xs:attribute name="UpdateCheck" type="UpdateCheck" use="optional" />
    <xs:attribute name="IsDiscriminator" type="xs:boolean" use="optional" />
    <xs:attribute name="Expression" type="xs:string" use="optional" />
    <xs:attribute name="IsVersion" type="xs:boolean" use="optional" />
    <xs:attribute name="AutoSync" type="AutoSync" use="optional" />
  </xs:complexType>
  <xs:complexType name="Association">
    <xs:attribute name="Name" type="xs:string" use="optional" />
    <xs:attribute name="Member" type="xs:string" use="required" />
    <xs:attribute name="Storage" type="xs:string" use="optional" />
    <xs:attribute name="ThisKey" type="xs:string" use="optional" />
    <xs:attribute name="OtherKey" type="xs:string" use="optional" />
    <xs:attribute name="IsForeignKey" type="xs:boolean" use="optional" />
    <xs:attribute name="IsUnique" type="xs:boolean" use="optional" />
    <xs:attribute name="DeleteRule" type="xs:string" use="optional" />
    <xs:attribute name="DeleteOnNull" type="xs:boolean" use="optional" />
  </xs:complexType>
  <xs:complexType name="Function">
    <xs:sequence>
      <xs:element name="Parameter" type="Parameter" minOccurs="0" maxOccurs="unbounded" />
      <xs:choice>
        <xs:element name="ElementType" type="Type" minOccurs="0" maxOccurs="unbounded" />
        <xs:element name="Return" type="Return" minOccurs="0" maxOccurs="1" />
      </xs:choice>
    </xs:sequence>
    <xs:attribute name="Name" type="xs:string" use="optional" />
    <xs:attribute name="Method" type="xs:string" use="required" />
    <xs:attribute name="IsComposable" type="xs:boolean" use="optional" />
  </xs:complexType>
  <xs:complexType name="Parameter">
    <xs:attribute name="Name" type="xs:string" use="optional" />
    <xs:attribute name="Parameter" type="xs:string" use="required" />
    <xs:attribute name="DbType" type="xs:string" use="optional" />
    <xs:attribute name="Direction" type="ParameterDirection" use="optional" />
  </xs:complexType>
  <xs:complexType name="Return">
    <xs:attribute name="DbType" type="xs:string" use="optional" />
  </xs:complexType>
  <xs:simpleType name="UpdateCheck">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Always" />
      <xs:enumeration value="Never" />
      <xs:enumeration value="WhenChanged" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="ParameterDirection">
    <xs:restriction base="xs:string">
      <xs:enumeration value="In" />
      <xs:enumeration value="Out" />
      <xs:enumeration value="InOut" />
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="AutoSync">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Never" />
      <xs:enumeration value="OnInsert" />
      <xs:enumeration value="OnUpdate" />
      <xs:enumeration value="Always" />
      <xs:enumeration value="Default" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

Community Additions

ADD
Show:
© 2014 Microsoft