扩展属性 (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>

PropertyKindMetadataProperty 对象上的枚举,可用于标识 EDM 中的系统属性和扩展属性。有关在代码中使用此枚举的示例,请参见使用 AdventureWorks 对象模型 (EDM)

运行使用扩展属性的代码

可以使用 AdventureWorks 完整模型 (EDM) 中的数据模型和应用程序代码测试具有扩展属性的架构修改。可将修改添加到 csdl 架构并使用 Edmgen.exe 重新生成对象模型,如 AdventureWorks 主题中所述。

然后将下面的代码添加到主题使用 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)