共用方式為


How to: Define a Model with a Horizontal Partition in the Storage Model (Entity Framework)

The horizontal partition in this example maps a single entity type in the conceptual model to two tables in the storage model. A single Order entity in the conceptual model is supported by two tables in the data source that contain regular orders and priority orders. Both tables contain the same columns. A condition that is defined in the mapping specification determines how to partition the data between the tables.

The example is based on data in the Northwind.

Note

The ADO.NET Entity Data Model Designer does not support this mapping scenario. The model must be implemented by using EdmGen.exe. For more information about EdmGen.exe, see EDM Generator (EdmGen.exe).

To implement the conceptual schema definition language (CSDL) requirements and build the programmable object model

  1. Create a CSDL file that contains the following content:

    <?xml version="1.0" encoding="utf-8"?>
     <Schema xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
              Namespace="Test.EntitySplitting.Model" Alias="Self">
        <EntityContainer Name="Test_EntitySplitting_Model_Northwind">
          <EntitySet Name="COrder1" EntityType="Self.COrder" />
        </EntityContainer>
        <EntityType Name="COrder">
          <Key>
            <PropertyRef Name="OrderID" />
          </Key>
          <Property Name="OrderID" Type="Int32" Nullable="false" />
          <Property Name="OrderDesc" Type="String" MaxLength="1024" />
          <Property Name="IsPriority" Type="Boolean" Nullable="false" />
        </EntityType>
     </Schema>
    
  2. Generate the C# or Visual Basic code from which the programmable object model will be built. To do this run the following EdmGen.exe command on a single line:

    C:\Program Files\Microsoft Visual Studio 9.0\VC>edmgen
     /mode:EntityClassGeneration 
    /incsdl:"C:\EDMGenFiles\NorthwindHorzSplit.csdl" 
    /outobjectlayer:"NorthwindHorzSplit.cs"
    
  3. Add the output file, NorthwindHorzSplit.cs, to a Visual Studio class library project.

  4. In the project, add references to the System.Data.Entity and System.Runtime.Serialization libraries.

  5. Build the project.

To implement the store schema definition language (SSDL) requirements

  1. Create an SSDL file that contains the following content:

    <?xml version="1.0" encoding="utf-8"?>
    <Schema Namespace="Test.EntitySplitting.Target" Alias="Self"
            Provider="System.Data.SqlClient"
            ProviderManifestToken="2005"
            xmlns:edm="https://schemas.microsoft.com/ado/2006/04/edm/ssdl"
            xmlns="https://schemas.microsoft.com/ado/2006/04/edm/ssdl">
    
      <EntityContainer Name="Test_EntitySplitting_Target_Northwind">
        <EntitySet Name="SRegularOrders1" EntityType="Self.SRegularOrders"
                   Schema="dbo" Table="SRegularOrders" />
        <EntitySet Name="SPriorityOrders1" EntityType="Self.SPriorityOrders"
                   Schema="dbo" Table="SPriorityOrders" />
      </EntityContainer>
      <EntityType Name="SRegularOrders">
        <Key>
          <PropertyRef Name="OrderId" />
        </Key>
        <Property Name="OrderId" Type="int" Nullable="false" />
        <Property Name="OrderDesc" Type="nvarchar" MaxLength="1024" />
        <Property Name="IsPriority" Type="bit" Nullable="false" />
      </EntityType>
      <EntityType Name="SPriorityOrders">
        <Key>
          <PropertyRef Name="OrderId" />
        </Key>
        <Property Name="OrderId" Type="int" Nullable="false" />
        <Property Name="OrderDesc" Type="nvarchar" MaxLength="1024" />
        <Property Name="IsPriority" Type="bit" Nullable="false" />
      </EntityType>
    </Schema>
    
  2. This SSDL file will be required by any code that uses the object model that is defined in the earlier procedure.

  3. Add two tables to the Northwind database that support the SSDL entities SRegularOrders and SPriorityOrders. Use data types that correspond to the types in SSDL that are specified in Simple Types (EDM).

To implement the mapping specification language (MSL) requirements

  1. Create an MSL file that contains the following content:

    <?xml version="1.0" encoding="utf-8"?>
     <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S">
        <Alias Key="CNorthwind" Value="Test.EntitySplitting.Model" />
        <Alias Key="SNorthwind" Value="Test.EntitySplitting.Target" />
    
        <EntityContainerMapping CdmEntityContainer="Test_EntitySplitting_Model_Northwind"
                                StorageEntityContainer="Test_EntitySplitting_Target_Northwind">
          <EntitySetMapping Name="COrder1" TypeName="CNorthwind.COrder">
            <MappingFragment StoreEntitySet="SRegularOrders1">
              <ScalarProperty Name="OrderID" ColumnName="OrderId" />
              <ScalarProperty Name="OrderDesc" ColumnName="OrderDesc" />
              <ScalarProperty Name ="IsPriority" ColumnName="IsPriority"/>
              <Condition Name="IsPriority" Value="false" />
            </MappingFragment>
            <MappingFragment StoreEntitySet="SPriorityOrders1">
              <ScalarProperty Name="OrderID" ColumnName="OrderId" />
              <ScalarProperty Name="OrderDesc" ColumnName="OrderDesc" />
              <ScalarProperty Name="IsPriority" ColumnName="IsPriority"/>
              <Condition Name="IsPriority" Value="true" />
            </MappingFragment>
          </EntitySetMapping>
        </EntityContainerMapping>
      </Mapping>
    
  2. This MSL file will be required by any code that uses the object model that is defined in the CSDL procedure.

See Also

Concepts

Entity Data Model Mapping Scenarios (Application Scenarios)