Condividi tramite


Procedura: definire un modello con una singola entità mappata a due tabelle

Quando si utilizzano dati legacy in Entity Data Model (EDM), è talvolta utile essere in grado di eseguire il mapping di una singola entità a due tabelle nel database. È possibile eseguire questa operazione quando due tabelle condividono una chiave comune, come nel caso delle tabelle Customer e Store del database di esempio AdventureWorks fornito con SQL Server 2005.

Creare un modello EDM che utilizza le tabelle Customer e Store del database AdventureWorks e modificare il file con estensione edmx, come illustrato nelle sezioni seguenti. Utilizzare il codice incluso nell'argomento Procedura: creare ed eseguire query di oggetto utilizzando un'entità mappata a tabelle distinte per eseguire il test del modello di dati.

Per implementare i requisiti CSDL (Conceptual Schema Definition Language)

  1. Individuare la sezione <edmx:ConceptualModels> del file con estensione edmx.

  2. Rimuovere i tag <EntityType> che rappresentano l'entità Customer.

  3. Rimuovere i tag <Key> e <PropertyRef> e il nome e l'identificatore StoreID dell'entità Customer ora eliminata.

  4. Spostare le proprietà dell'entità Customer eliminata all'interno dei tag <EntityType> per l'entità Store.

  5. Rinominare la proprietà rowguid dell'entità Customer ora eliminata in Cust_rowguid.

  6. Rinominare la proprietà ModifiedDate dell'entità Customer ora eliminata in Cust_ModifiedDate.

  7. Di seguito viene illustrato lo schema concettuale.

   <edmx:ConceptualModels>
      <Schema Namespace="AdventureWorksModel" Alias="Self"
            xmlns="https://schemas.microsoft.com/ado/2006/04/edm">
        <EntityContainer Name="AdventureWorksEntities">
          
          <EntitySet Name="Store"
              EntityType="AdventureWorksModel.Store" />
        </EntityContainer>
        
        <EntityType Name="Store">
          <Key>
            <PropertyRef Name="CustomerID" />
          </Key>
          <Property Name="CustomerID" Type="Int32" Nullable="false" />
          <Property Name="Name" Type="String" 
                  Nullable="false" MaxLength="50" />
          <Property Name="SalesPersonID" Type="Int32" />
          <Property Name="Demographics" Type="String"
                     MaxLength="1073741823" />
          <Property Name="rowguid" Type="Guid" Nullable="false" />
          <Property Name="ModifiedDate" Type="DateTime"
                     Nullable="false" />
          
          <Property Name="TerritoryID" Type="Int32" />
          <Property Name="AccountNumber" Type="String" 
                  Nullable="false" 
                    MaxLength="10" Unicode="false" />
          <Property Name="CustomerType" Type="String" 
                  Nullable="false" 
                    MaxLength="1" FixedLength="true" />
          <Property Name="Cust_rowguid" Type="Guid" 
                  Nullable="false" />
          <Property Name="Cust_ModifiedDate" Type="DateTime"
                  Nullable="false" />
        </EntityType>
        
      </Schema>
    </edmx:ConceptualModels>

Per implementare i requisiti SSDL (Store Schema Definition Language)

  1. Individuare la sezione <edmx:StorageModels> del file con estensione edmx.

  2. Lasciare lo schema SSDL invariato, come illustrato di seguito:

    <edmx:StorageModels>
      <Schema Namespace="AdventureWorksModel.Store" Alias="Self"
          Provider="System.Data.SqlClient"
          ProviderManifestToken="2005"
          xmlns="https://schemas.microsoft.com/ado/2006/04/edm/ssdl">
        <EntityContainer Name="Sales">
          <EntitySet Name="Customer"
                EntityType="AdventureWorksModel.Store.Customer" />
          <EntitySet Name="Store"
              EntityType="AdventureWorksModel.Store.Store" />
          <AssociationSet Name="FK_Store_Customer_CustomerID"
  Association="AdventureWorksModel.Store.FK_Store_Customer_CustomerID">
            <End Role="Customer" EntitySet="Customer" />
            <End Role="Store" EntitySet="Store" />
          </AssociationSet>
        </EntityContainer>
        <EntityType Name="Customer">
          <Key>
            <PropertyRef Name="CustomerID" />
          </Key>
          <Property Name="CustomerID" Type="int" 
                Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="TerritoryID" Type="int" />
          <Property Name="AccountNumber" Type="varchar"
                           Nullable="false" MaxLength="10" />
          <Property Name="CustomerType" Type="nchar" 
                Nullable="false" MaxLength="1" />
          <Property Name="rowguid" 
                    Type="uniqueidentifier" Nullable="false" />
          <Property Name="ModifiedDate" Type="datetime"
                Nullable="false" />
        </EntityType>
        <EntityType Name="Store">
          <Key>
            <PropertyRef Name="CustomerID" />
          </Key>
          <Property Name="CustomerID" Type="int" Nullable="false" />
          <Property Name="Name" Type="nvarchar" 
                Nullable="false" MaxLength="50" />
          <Property Name="SalesPersonID" Type="int" />
          <Property Name="Demographics" Type="xml" />
          <Property Name="rowguid" Type="uniqueidentifier"
                Nullable="false" />
          <Property Name="ModifiedDate" Type="datetime"
                         Nullable="false" />
        </EntityType>
        <Association Name="FK_Store_Customer_CustomerID">
          <End Role="Customer"
        Type="AdventureWorksModel.Store.Customer" Multiplicity="1" />
          <End Role="Store" Type="AdventureWorksModel.Store.Store"
                Multiplicity="0..1" />
          <ReferentialConstraint>
            <Principal Role="Customer">
              <PropertyRef Name="CustomerID" />
            </Principal>
            <Dependent Role="Store">
              <PropertyRef Name="CustomerID" />
            </Dependent>
          </ReferentialConstraint>
        </Association>
      </Schema>
    </edmx:StorageModels>

Per implementare i requisiti MSL (Mapping Specification Language)

  1. Individuare la sezione <edmx:Mappings> del file con estensione edmx.

  2. Rimuovere i tag <EntitySetMapping> per il set di entità Customer dalla specifica di mapping.

  3. Rimuovere i tag <EntityTypeMapping> per il tipo di entità Customer dalla specifica di mapping.

  4. Spostare <MappingFragment> per StoreEntitySet di Customer all'interno del mapping EntityType per StoreEntitySet.

  5. Di seguito viene illustrata la specifica di mapping.

    <edmx:Mappings>
      <Mapping Space="C-S"
          xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
        <EntityContainerMapping StorageEntityContainer="Sales"
          CdmEntityContainer="AdventureWorksEntities">
          
          <EntitySetMapping Name="Store">
            <EntityTypeMapping
                 TypeName="IsTypeOf(AdventureWorksModel.Store)">
              <MappingFragment StoreEntitySet="Store">
                <ScalarProperty Name="CustomerID"
                           ColumnName="CustomerID" />
                <ScalarProperty Name="Name" ColumnName="Name" />
                <ScalarProperty Name="SalesPersonID"
                           ColumnName="SalesPersonID" />
                <ScalarProperty Name="Demographics"
                           ColumnName="Demographics" />
                <ScalarProperty Name="rowguid"
                           ColumnName="rowguid" />
                <ScalarProperty Name="ModifiedDate"
                           ColumnName="ModifiedDate" />
              </MappingFragment>
              <MappingFragment StoreEntitySet="Customer">
                <ScalarProperty Name="CustomerID"
                           ColumnName="CustomerID" />
                <ScalarProperty Name="TerritoryID"
                           ColumnName="TerritoryID" />
                <ScalarProperty Name="AccountNumber"
                           ColumnName="AccountNumber" />
                <ScalarProperty Name="CustomerType"
                           ColumnName="CustomerType" />
                <ScalarProperty Name="Cust_rowguid"
                           ColumnName="rowguid" />
                <ScalarProperty Name="Cust_ModifiedDate"
                           ColumnName="ModifiedDate" />
              </MappingFragment>
            </EntityTypeMapping>
          </EntitySetMapping>
          
        </EntityContainerMapping>
      </Mapping>
    </edmx:Mappings>

Vedere anche

Attività

Procedura: creare ed eseguire query di oggetto utilizzando un'entità mappata a tabelle distinte