LINQ to SQL 支援 外部對應,此程式可讓您使用個別的 XML 檔案來指定資料庫數據模型與物件模型之間的對應。 使用外部對應檔案的優點包括:
您可以將對應程式碼保持在應用程式碼之外。 此方法可減少應用程式程式代碼中的雜亂。
您可以將外部對應檔視為組態檔。 例如,您可以藉由交換外部對應檔案,來更新應用程式在傳送二進位檔之後的行為。
需求
對應檔案必須是 XML 檔案,而且該檔案必須針對 LINQ to SQL 架構定義 (.xsd) 檔案進行驗證。
適用的規則如下:
對應檔案必須是 XML 檔案。
XML 對應檔必須對 XML 架構定義檔有效。 如需詳細資訊,請參閱 如何:驗證 DBML 和外部對應檔案。
外部對應會覆寫屬性型對應。 換句話說,當您使用外部映射來源建立 DataContext時,DataContext 會忽略您在類別上設定的所有映射屬性。 不論類別是否包含在外部對應檔中,此行為都成立。
LINQ to SQL 不支援使用這兩種對應方法的混合式用法(屬性型和外部方法)。
XML 架構定義檔案
LINQ to SQL 中的外部對應必須針對下列 XML 架構定義有效。
區分此架構定義檔案與用來驗證 DBML 檔案的架構定義檔案。 如需詳細資訊,請參閱 LINQ to SQL 中的程式碼產生。
備註
Visual Studio 使用者也會在 [XML 架構] 對話方塊中找到這個 XSD 檔案,做為 “LinqToSqlMapping.xsd”。 若要正確使用此檔案來驗證外部對應檔案,請參閱 如何:驗證 DBML 和外部對應檔案。
<?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>