Delen via


Annotation Attributes (CSDL)

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

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

  • More than one annotation attribute may be applied to a given CSDL element.

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

Annotation attributes can be used to provide extra metadata about the elements in a conceptual model. 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 attribute (CustomAttribute). The example also shows an annotation element applied to the entity type element. For more information, see Annotation Elements (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 attribute 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:CustomAttribute") Then
    Dim annotationProperty As MetadataProperty = _
        contentType.MetadataProperties("http://CustomNamespace.com:CustomAttribute")
    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:CustomAttribute"))
{
    MetadataProperty annotationProperty =
        contentType.MetadataProperties["http://CustomNamespace.com:CustomAttribute"];
    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 Elements (CSDL)
CSDL Specification

Other Resources

CSDL, SSDL, and MSL Specifications