Поделиться через


AssociationSetMapping Element (MSL)

Mapping an association in the Entity Data Model (EDM) identifies columns in data tables that correspond to EndProperty elements of related entities. For entities in the conceptual schema to be related, there must be a foreign key column in the data table that contains some property of another table. Usually this foreign key column corresponds to the key property an entity.

An AssociationSetMapping is specified inside an EntityContainerMapping element. The following declaration of the Employee_Employee_ManagerID AssociationSetMapping defines the mapping of the AssociationSet and TypeName defined in the AdventureWorksHRModel conceptual schema.

Because both related entities are Employee entities, one table contains data for both entities. The EndProperty elements in this example specify the role of each related entity defined in the conceptual schema, and ScalarProperty elements specify a property of each entity and the ColumnName to which this property corresponds. The ends of this association are both Employee entities and the identifying property in each case is linked to a corresponding EmployeeID column of the Employee data table. The condition specifies that the ManagerID column of the second Employee must be assigned a value or the Association is not valid.

<?xml version="1.0" encoding="utf-8"?>
<Mapping Space="C-S" 
    xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
  <EntityContainerMapping CdmEntityContainer="HumanResources" 
StorageEntityContainer="HumanResources">

    <AssociationSetMapping Name="Employee_Employee_ManagerID" 
TypeName="AdventureWorksHRModel.Employee_Employee_ManagerID" 
StoreEntitySet="Employee">
      <EndProperty Name="Employee">
        <ScalarProperty Name="EmployeeID" ColumnName="ManagerID" />
      </EndProperty>
      <EndProperty Name="EmployeeManager">
        <ScalarProperty Name="EmployeeID" ColumnName="EmployeeID" />
      </EndProperty>
      <Condition ColumnName="ManagerID" IsNull="false" />
    </AssociationSetMapping>

  </EntityContainerMapping>

In this example, the AssociationSetMapping element is part of the AdventureWorksHRModel schema. The HumanResources EntityContainerMapping element of this AssociationSetMapping maps the Employee_Employee_ManagerID association. The association relates an employee entity to another employee entity designated as manager.

This mapping depends on the Condition specified in the final line of the AssociationSetMapping. For the association to be valid, it must connect an Employee entity with another Employee entity designated as manager by a foreign key value in the ManagerID column of the Employee table. If this column is not assigned a value, the condition is not met, and assigning additional details in this association would be meaningless. If an association of this kind is created in code when the ManagerID value is Null, the association throws an exception when the SaveChanges method is called.

See Also

Concepts

EntityContainerMapping Element (MSL)
Association Element (CSDL)
AssociationSet Element (EntityContainer CSDL)
Association Element (SSDL)
AssociationSet Element (EntityContainer SSDL)
AdventureWorks Complete Model (EDM)