Bagikan melalui


Extended Property (CSDL)

In the Entity Data Model (EDM), an extended property is a property that is defined and exists in a user's namespace rather than in the system namespace identified by xmlns="https://schemas.microsoft.com/ado/2006/04/edm". Conceptual schema definition language (CSDL) is used to define both kinds of properties. To add an extended property to a CSDL schema, define a namespace and then use it in the definition of an entity type and its corresponding entity set.

The following example defines a namespace xmlns:o1="https://other.contoso.com/schema". The namespace prefix: o1 serves as an alias in the definitions of the entity type AWBuildVersion and the entity set AWBuildVersions.

<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="AdventureWorksModel" Alias="Self" 
        xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
        xmlns:o1="https://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 is an enumeration found on MetadataProperty objects that can be used to identify system properties and extended properties in the EDM. For an example using this enumeration in code, see Using the AdventureWorks Object Model (EDM).

Running Code that uses Extended Properties

The schema modifications with extended properties can be tested by using the data model and application code in AdventureWorks Complete Model (EDM). Add the modifications to the csdl schema and rebuild the object model with Edmgen.exe as described in the AdventureWorks topic.

Then add the following code to the client application in the topic Using the AdventureWorks Object Model (EDM). These methods should be added to the Program class and called from the code in the comment that references the DisplayProperties function.

        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));
            }
        }

See Also

Concepts

AdventureWorks Complete Conceptual Schema (EDM)
AdventureWorks Complete Storage Schema (EDM)
AdventureWorks Complete Mapping Schema (EDM)
Using the AdventureWorks Object Model (EDM)