在 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 物件上找到的列舉型別 (Enumeration),這些物件可用來識別 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)