次の方法で共有


Table-Per-Hierarchy 継承でモデルを定義する方法 (Entity Framework)

このトピックでは、Table-Per-Hierarchy 継承を持つ概念モデルを手動で作成する方法について説明します。 Table-Per-Hierarchy 継承では、1 つのデータベース テーブルを使用して、継承階層のすべてのエンティティ型のデータを維持します。

Bb738443.note(ja-jp,VS.100).gif注 :
Table-Per-Type 継承を持つモデルを定義するには、ADO.NET Entity Data Model Toolsを使用することをお勧めします。詳細については、「Walkthrough: Mapping Inheritance - Table-per-Hierarchy」を参照してください。

Table-Per-Hierarchy 継承を持つモデルを手動で定義するための基本手順は、次のとおりです。

  1. 基本エンティティ型および派生型を含む概念モデルで 1 つのエンティティ セットを定義します。 詳細については、「EntitySet 要素 (CSDL)」を参照してください。

  2. BaseType 属性を使用して概念モデルの派生エンティティ型を定義し、派生型の非継承プロパティのみを定義します。 詳細については、「EntityType 要素 (CSDL)」を参照してください。

  3. 基になるデータベース テーブルから、基本型と派生型の判別に使用される値を持つ列を選択します。 たとえば、Employee テーブルに整数値の EmployeeType 列が含まれているとき、Employee 基本エンティティ型が HourlyEmployee 派生型と SalariedEmployee 派生型のどちらかである場合に、この列の値を型の判別に使用できます。 詳細については、次の例を参照してください。

    判別用の列は条件の一部としてマップされるため、階層内にあるいずれかのエンティティ型の対応するプロパティを格納することはできません。 ただし、条件で Is Null 比較または Is Not Null 比較を使用する場合は例外です。 この場合は、判別用の列にエンティティ型の対応するプロパティを格納できます。

    判別用の列に 3 つ以上の値 (整数型など) を格納できる場合は、列で null 値を許容するか、既定値を割り当てる必要があります。 これにより、新しい型が作成されてデータベースに保存されたときに、列を null にすることも、値を格納することもできるようになります。

    条件を使用して、階層内の各派生型をマップする必要があります。また、条件を使用して基本型をマップすることもできます。 基本型が抽象型の場合、マッピングおよび条件は使用できません。

  4. マッピング スキーマ言語 (MSL) の同じ EntitySetMapping 要素で、基本エンティティ型と派生型をマップします。 継承プロパティを、該当するテーブルの列にマップします。 派生型の TypeName 属性の値を設定する場合、IsTypeOf 構文を使用します。 マッピング条件を使用して階層内の型を判別します。 詳細については、「EntitySetMapping 要素 (MSL)」および「Condition 要素 (MSL)」を参照してください。

次の例では、School サンプル データベースがインストールされ、Entity Framework を使用するようにプロジェクトが手動で構成されていることを前提としています。 詳細については、「School サンプル データベースの作成 (Entity Framework クイック スタート)」および「Entity Framework の構成 (Entity Framework タスク)」を参照してください。

ストレージ モデルを作成するには

  1. 次の XML ファイルをプロジェクトに追加し、AdventureWorks.ssdl という名前を付けます。

    <?xml version="1.0" encoding="utf-8" ?>
    <Schema Namespace="AdventureWorksModel.Store" Alias="Self" Provider="System.Data.SqlClient"
                ProviderManifestToken="2008"
                xmlns:store="https://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator"
                xmlns="https://schemas.microsoft.com/ado/2009/02/edm/ssdl">
      <EntityContainer Name="AdventureWorksModelStoreContainer">
        <EntitySet Name="Product" EntityType="AdventureWorksModel.Store.Product"
                   store:Type="Tables" Schema="Production" />
      </EntityContainer>
      <EntityType Name="Product">
        <Key>
          <PropertyRef Name="ProductID" />
        </Key>
        <Property Name="ProductID" Type="int" Nullable="false"
                  StoreGeneratedPattern="Identity" />
        <Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />
        <Property Name="ProductNumber" Type="nvarchar" Nullable="false" MaxLength="25" />
        <Property Name="MakeFlag" Type="bit" Nullable="false" />
        <Property Name="FinishedGoodsFlag" Type="bit" Nullable="false" />
        <Property Name="Color" Type="nvarchar" MaxLength="15" />
        <Property Name="SafetyStockLevel" Type="smallint" Nullable="false" />
        <Property Name="ReorderPoint" Type="smallint" Nullable="false" />
        <Property Name="StandardCost" Type="money" Nullable="false" />
        <Property Name="ListPrice" Type="money" Nullable="false" />
        <Property Name="Size" Type="nvarchar" MaxLength="5" />
        <Property Name="SizeUnitMeasureCode" Type="nchar" MaxLength="3" />
        <Property Name="WeightUnitMeasureCode" Type="nchar" MaxLength="3" />
        <Property Name="Weight" Type="decimal" Precision="8" Scale="2" />
        <Property Name="DaysToManufacture" Type="int" Nullable="false" />
        <Property Name="ProductLine" Type="nchar" MaxLength="2" />
        <Property Name="Class" Type="nchar" MaxLength="2" />
        <Property Name="Style" Type="nchar" MaxLength="2" />
        <Property Name="ProductSubcategoryID" Type="int" />
        <Property Name="ProductModelID" Type="int" />
        <Property Name="SellStartDate" Type="datetime" Nullable="false" />
        <Property Name="SellEndDate" Type="datetime" />
        <Property Name="DiscontinuedDate" Type="datetime" />
        <Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
        <Property Name="ModifiedDate" Type="datetime" Nullable="false" />
      </EntityType>
    </Schema>
    

概念モデルを作成するには

  1. 次の XML ファイルをプロジェクトに追加し、AdventureWorks.csdl という名前を付けます。 次の点に注意してください。

    • Product および DiscontinuedProduct という 2 つのエンティティ型に対して 1 つのエンティティ セット Products のみが定義されています。

    • 定義の BaseType 属性で示されているように、DiscontinuedProduct エンティティ型は派生型です。

    • DiscontinuedProduct エンティティ型で定義されるプロパティは、非継承プロパティだけです。

    • ストレージ モデルの MakeFlag 列 (上の AdventureWorks.ssdl ファイルを参照) は、基本型または派生型のプロパティとして表示されません。 この列の値は、条件の一部としてマップされる場合のように、階層内の型を判別するために使用されます (下の AdventureWorks.msl ファイルを参照)。

    Bb738443.note(ja-jp,VS.100).gif注 :
    列が Is Null 条件または Is Not Null 条件で使用される場合、対応するプロパティはエンティティ型に表示されます。

<?xml version="1.0" encoding="utf-8" ?>
<Schema Namespace="AdventureWorksModel" Alias="Self"
              xmlns:annotation="https://schemas.microsoft.com/ado/2009/02/edm/annotation"
              xmlns="https://schemas.microsoft.com/ado/2008/09/edm">
  <EntityContainer Name="AdventureWorksEntities" annotation:LazyLoadingEnabled="true">
    <EntitySet Name="Products" EntityType="AdventureWorksModel.Product" />
  </EntityContainer>
  <EntityType Name="Product">
    <Key>
      <PropertyRef Name="ProductID" />
    </Key>
    <Property Type="Int32" Name="ProductID" Nullable="false" />
    <Property Type="String" Name="Name" Nullable="false" MaxLength="50"
              FixedLength="false" Unicode="true" />
    <Property Type="String" Name="ProductNumber" Nullable="false" MaxLength="25"
              FixedLength="false" Unicode="true" />
    <Property Type="Boolean" Name="FinishedGoodsFlag" Nullable="false" />
    <Property Type="String" Name="Color" MaxLength="15" FixedLength="false"
              Unicode="true" />
    <Property Type="Int16" Name="SafetyStockLevel" Nullable="false" />
    <Property Type="Int16" Name="ReorderPoint" Nullable="false" />
    <Property Type="Decimal" Name="StandardCost" Nullable="false"
              Precision="19" Scale="4" />
    <Property Type="Decimal" Name="ListPrice" Nullable="false"
              Precision="19" Scale="4" />
    <Property Type="String" Name="Size" MaxLength="5" FixedLength="false"
              Unicode="true" />
    <Property Type="String" Name="SizeUnitMeasureCode" MaxLength="3"
              FixedLength="true" Unicode="true" />
    <Property Type="String" Name="WeightUnitMeasureCode" MaxLength="3"
              FixedLength="true" Unicode="true" />
    <Property Type="Decimal" Name="Weight" Precision="8" Scale="2" />
    <Property Type="Int32" Name="DaysToManufacture" Nullable="false" />
    <Property Type="String" Name="ProductLine" MaxLength="2"
              FixedLength="true" Unicode="true" />
    <Property Type="String" Name="Class" MaxLength="2" FixedLength="true"
              Unicode="true" />
    <Property Type="String" Name="Style" MaxLength="2" FixedLength="true"
              Unicode="true" />
    <Property Type="Int32" Name="ProductSubcategoryID" />
    <Property Type="Int32" Name="ProductModelID" />
    <Property Type="DateTime" Name="SellStartDate" Nullable="false" />
    <Property Type="DateTime" Name="SellEndDate" />
    <Property Type="Guid" Name="rowguid" Nullable="false" />
    <Property Type="DateTime" Name="ModifiedDate" Nullable="false" />
  </EntityType>
  <EntityType Name="DiscontinuedProduct" BaseType="AdventureWorksModel.Product" >
    <Property Type="DateTime" Name="DiscontinuedDate" />
  </EntityType>
</Schema>

概念モデルとストレージ モデルの間のマッピングを定義するには

  1. 次の XML ファイルをプロジェクトに追加し、AdventureWorks.msl という名前を付けます。 次の点に注意してください。

    • Product エンティティ型と DiscontinuedProduct エンティティ型のマッピングは、同じ EntitySetMapping 要素で定義されています。

    • DiscontinuedProduct の継承されたプロパティは、基になるデータベース テーブル内の対応する列にマップされます。

    • DiscontinuedProduct 派生型の型を示すために、IsTypeOf 構文を使用します。

    • 判別用の列 MakeFlag は、階層内のエンティティ型ごとに Condition 要素でマップされます。

    <?xml version="1.0" encoding="utf-8" ?>
    <Mapping Space="C-S" xmlns="https://schemas.microsoft.com/ado/2008/09/mapping/cs">
      <EntityContainerMapping StorageEntityContainer="AdventureWorksModelStoreContainer"
                              CdmEntityContainer="AdventureWorksEntities">
        <EntitySetMapping Name="Products">
          <EntityTypeMapping TypeName="AdventureWorksModel.Product">
            <MappingFragment StoreEntitySet="Product">
              <ScalarProperty Name="ProductID" ColumnName="ProductID" />
              <ScalarProperty Name="ModifiedDate" ColumnName="ModifiedDate" />
              <ScalarProperty Name="rowguid" ColumnName="rowguid" />
              <ScalarProperty Name="SellEndDate" ColumnName="SellEndDate" />
              <ScalarProperty Name="SellStartDate" ColumnName="SellStartDate" />
              <ScalarProperty Name="ProductModelID" ColumnName="ProductModelID" />
              <ScalarProperty Name="ProductSubcategoryID" ColumnName="ProductSubcategoryID" />
              <ScalarProperty Name="Style" ColumnName="Style" />
              <ScalarProperty Name="Class" ColumnName="Class" />
              <ScalarProperty Name="ProductLine" ColumnName="ProductLine" />
              <ScalarProperty Name="DaysToManufacture" ColumnName="DaysToManufacture" />
              <ScalarProperty Name="Weight" ColumnName="Weight" />
              <ScalarProperty Name="WeightUnitMeasureCode" ColumnName="WeightUnitMeasureCode" />
              <ScalarProperty Name="SizeUnitMeasureCode" ColumnName="SizeUnitMeasureCode" />
              <ScalarProperty Name="Size" ColumnName="Size" />
              <ScalarProperty Name="ListPrice" ColumnName="ListPrice" />
              <ScalarProperty Name="StandardCost" ColumnName="StandardCost" />
              <ScalarProperty Name="ReorderPoint" ColumnName="ReorderPoint" />
              <ScalarProperty Name="SafetyStockLevel" ColumnName="SafetyStockLevel" />
              <ScalarProperty Name="Color" ColumnName="Color" />
              <ScalarProperty Name="FinishedGoodsFlag" ColumnName="FinishedGoodsFlag" />
              <ScalarProperty Name="ProductNumber" ColumnName="ProductNumber" />
              <ScalarProperty Name="Name" ColumnName="Name" />
              <Condition ColumnName="MakeFlag" Value="0" />
            </MappingFragment>
          </EntityTypeMapping>
          <EntityTypeMapping TypeName="IsTypeOf(AdventureWorksModel.DiscontinuedProduct)">
            <MappingFragment StoreEntitySet="Product">
              <ScalarProperty Name="ModifiedDate" ColumnName="ModifiedDate" />
              <ScalarProperty Name="rowguid" ColumnName="rowguid" />
              <ScalarProperty Name="SellEndDate" ColumnName="SellEndDate" />
              <ScalarProperty Name="SellStartDate" ColumnName="SellStartDate" />
              <ScalarProperty Name="ProductModelID" ColumnName="ProductModelID" />
              <ScalarProperty Name="ProductSubcategoryID" ColumnName="ProductSubcategoryID" />
              <ScalarProperty Name="Style" ColumnName="Style" />
              <ScalarProperty Name="Class" ColumnName="Class" />
              <ScalarProperty Name="ProductLine" ColumnName="ProductLine" />
              <ScalarProperty Name="DaysToManufacture" ColumnName="DaysToManufacture" />
              <ScalarProperty Name="Weight" ColumnName="Weight" />
              <ScalarProperty Name="WeightUnitMeasureCode" ColumnName="WeightUnitMeasureCode" />
              <ScalarProperty Name="SizeUnitMeasureCode" ColumnName="SizeUnitMeasureCode" />
              <ScalarProperty Name="Size" ColumnName="Size" />
              <ScalarProperty Name="ListPrice" ColumnName="ListPrice" />
              <ScalarProperty Name="StandardCost" ColumnName="StandardCost" />
              <ScalarProperty Name="ReorderPoint" ColumnName="ReorderPoint" />
              <ScalarProperty Name="SafetyStockLevel" ColumnName="SafetyStockLevel" />
              <ScalarProperty Name="Color" ColumnName="Color" />
              <ScalarProperty Name="FinishedGoodsFlag" ColumnName="FinishedGoodsFlag" />
              <ScalarProperty Name="ProductNumber" ColumnName="ProductNumber" />
              <ScalarProperty Name="Name" ColumnName="Name" />
              <ScalarProperty Name="ProductID" ColumnName="ProductID" />
              <ScalarProperty Name="DiscontinuedDate" ColumnName="DiscontinuedDate" />
              <Condition ColumnName="MakeFlag" Value="1" />
            </MappingFragment>
          </EntityTypeMapping>
        </EntitySetMapping>
      </EntityContainerMapping>
    </Mapping>
    

参照

処理手順

Table-Per-Type 継承でモデルを定義する方法 (Entity Framework)

その他のリソース

高度なデータ モデルの定義 (Entity Framework タスク)
CSDL、SSDL、および MSL 仕様