拡張プロパティ (CSDL)

Entity Data Model (EDM) では、拡張プロパティとは、xmlns="https://schemas.microsoft.com/ado/2006/04/edm" によって識別されるシステム名前空間ではなく、ユーザーの名前空間で定義され、そこに存在するプロパティのことです。概念スキーマ定義言語 (CSDL) は、どちらの種類のプロパティを定義するためにも使用されます。拡張プロパティを CSDL スキーマに追加するには、名前空間を定義してから、エンティティ型とそれに対応するエンティティ セットの定義でその名前空間を使用します。

次の例では、xmlns:o1="http://other.contoso.com/schema" という名前空間が定義されます。この名前空間のプレフィックス o1 は、エンティティ型 AWBuildVersion とエンティティ セット AWBuildVersions の定義において別名の役割を果たします。

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="AdventureWorksModel" Alias="Self" 
        xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
        xmlns:o1="http://other.contoso.com/schema">

  <EntityContainer Name="AdventureWorksEntities"
          o1:MyCustomAttribute="MyCustomAttributeValue">

    <EntitySet Name="AWBuildVersions"
          EntityType="Adventureworks.AWBuildVersion"
          o1:AnotherAttribute="AnotherAttributeValue"/>

  </EntityContainer>

…...

<EntityType Name="AWBuildVersion">
          <Key>
            <PropertyRef Name="SystemInformationID" />
          </Key>
          <Property Name="SystemInformationID"
                      Type="Byte" Nullable="false" />
          <Property Name="Database_Version" Type="String"
                      Nullable="false" />
          <Property Name="VersionDate" 
                      Type="DateTime" Nullable="false" />
          <Property Name="ModifiedDate"
                      Type="DateTime" Nullable="false" />
        </EntityType>

PropertyKind は、MetadataProperty オブジェクトにある列挙体であり、EDM でシステム プロパティと拡張プロパティを識別するために使用されます。この列挙体を使用したコード例については、「AdventureWorks オブジェクト モデルの使用 (EDM)」を参照してください。

拡張プロパティを使用するコードの実行

拡張プロパティを使用したスキーマの変更は、AdventureWorks Complete Model (EDM) のデータ モデルおよびアプリケーション コードを使用して確認できます。「AdventureWorks」トピックでの説明に従って、変更を csdl スキーマに追加し、Edmgen.exe でオブジェクト モデルを再構築します。

AdventureWorks オブジェクト モデルの使用 (EDM)」トピックのクライアント アプリケーションに次のコードを追加します。これらのメソッドは、Program クラスに追加され、DisplayProperties 関数を参照するコメント内のコードから呼び出されます。

        public static void DisplayProperties(
        MetadataWorkspace workspace, DataSpace model)
        {
            // Get a collection of the entity containers.
            ReadOnlyCollection<EntityContainer> containers =
                workspace.GetItems<EntityContainer>(model);

            // Iterate through collection to get each entity container.
            foreach (EntityContainer container in containers)
            {
                // Display extended properties for the entity container.
                DisplayExtendedProperties(container);

                // Iterate through collection to get each entity set.
                foreach (EntitySetBase baseSet in container.BaseEntitySets)
                {
                    // Check if this instance is an EntitySet.
                    if (baseSet is EntitySet)
                    {
                        // Display extended properties for the entity set.
                        DisplayExtendedProperties(baseSet);
                    }
                }
            }

            // Get a collection of the entity types.
            ReadOnlyCollection<EntityType> entities =
                   workspace.GetItems<EntityType>(model);

            // Iterate through the collection to get each entity type.
            foreach (EntityType entity in entities)
            {
                // Display the extended properties for the entity type.
                DisplayExtendedProperties(entity);
            }
        }
        private static void DisplayExtendedProperties(MetadataItem item)
        {
            foreach (MetadataProperty property in item.MetadataProperties)
            {
                if (property.PropertyKind == PropertyKind.Extended)
                    Console.WriteLine(string.Format("\t{0}\t{1}\t{2}",
                      item.GetType().Name, property.Name, property.Value));
            }
        }

参照

概念

AdventureWorks の完全な概念スキーマ (EDM)
AdventureWorks の完全なストレージ スキーマ (EDM)
AdventureWorks の完全なマッピング スキーマ (EDM)
AdventureWorks オブジェクト モデルの使用 (EDM)