Udostępnij za pośrednictwem


Annotation Elements (CSDL)

Annotation elements in conceptual schema definition language (CSDL) are custom XML elements in the conceptual model. In addition to having valid XML structure, the following must be true of annotation elements:

  • Annotation elements must not be in any XML namespace that is reserved for CSDL.

  • More than one annotation element may be a child of a given CSDL element.

  • The fully-qualified names of any two annotation elements must not be the same.

  • Annotation elements must appear after all other child elements of a given CSDL element.

Annotation elements can be used to provide extra metadata about the elements in a conceptual model. Starting with the .NET Framework version 4, metadata contained in annotation elements can be accessed at runtime by using classes in the System.Data.Metadata.Edm namespace.

Example

The following example shows an EntityType element with an annotation element (CustomElement). The example also show an annotation attribute applied to the entity type element. For more information, see Annotation Attributes (CSDL).

<Schema Namespace="SchoolModel" Alias="Self"
        xmlns:annotation="https://schemas.microsoft.com/ado/2009/02/edm/annotation"
        xmlns="https://schemas.microsoft.com/ado/2008/09/edm">
  <EntityContainer Name="SchoolEntities" annotation:LazyLoadingEnabled="true">
    <EntitySet Name="People" EntityType="SchoolModel.Person" />
  </EntityContainer>
  <EntityType Name="Person" xmlns:p="http://CustomNamespace.com"
              p:CustomAttribute="Data here.">
    <Key>
      <PropertyRef Name="PersonID" />
    </Key>
    <Property Name="PersonID" Type="Int32" Nullable="false"
              annotation:StoreGeneratedPattern="Identity" />
    <Property Name="LastName" Type="String" Nullable="false"
              MaxLength="50" Unicode="true" FixedLength="false" />
    <Property Name="FirstName" Type="String" Nullable="false"
              MaxLength="50" Unicode="true" FixedLength="false" />
    <Property Name="HireDate" Type="DateTime" />
    <Property Name="EnrollmentDate" Type="DateTime" />
    <p:CustomElement>
      Custom metadata.
    </p:CustomElement>
  </EntityType>
</Schema>

The following code retrieves the metadata in the annotation element and writes it to the console:

Dim collection As New EdmItemCollection("School.csdl")
Dim workspace As New MetadataWorkspace()
workspace.RegisterItemCollection(collection)
Dim contentType As EdmType
workspace.TryGetType("Person", "SchoolModel", DataSpace.CSpace, contentType)
If contentType.MetadataProperties.Contains("http://CustomNamespace.com:CustomElement") Then
    Dim annotationProperty As MetadataProperty = _
        contentType.MetadataProperties("http://CustomNamespace.com:CustomElement")
    Dim annotationValue As Object = annotationProperty.Value
    Console.WriteLine(annotationValue.ToString())
End If
EdmItemCollection collection = new EdmItemCollection("School.csdl");
MetadataWorkspace workspace = new MetadataWorkspace();
workspace.RegisterItemCollection(collection);
EdmType contentType;
workspace.TryGetType("Person", "SchoolModel", DataSpace.CSpace, out contentType);
if (contentType.MetadataProperties.Contains("http://CustomNamespace.com:CustomElement"))
{
    MetadataProperty annotationProperty =
        contentType.MetadataProperties["http://CustomNamespace.com:CustomElement"];
    object annotationValue = annotationProperty.Value;
    Console.WriteLine(annotationValue.ToString());
}

The code above assumes that the School.csdl file is in the project's output directory and that you have added the following Imports and Using statements to your project:

Imports System.Data.Metadata.Edm
using System.Data.Metadata.Edm;

See Also

Concepts

Annotation Attributes (CSDL)
CSDL Specification

Other Resources

CSDL, SSDL, and MSL Specifications