Attributs d'annotation (CSDL)

Les attributs d'annotation dans le langage CSDL (Conceptual Schema Definition Language) sont des attributs XML personnalisés dans le modèle conceptuel.En plus d'avoir une structure XML valide, les attributs d'annotation doivent satisfaire les critères suivants :

  • Les attributs d'annotation ne doivent pas figurer dans un espace de noms XML réservé pour le langage CSDL.

  • Plusieurs attributs d'annotation peuvent être appliqués à un élément CSDL donné.

  • Les noms qualifiés complets de deux attributs d'annotation ne doivent pas être identiques.

Les attributs d'annotation peuvent être utilisés pour fournir des métadonnées supplémentaires sur des éléments dans un modèle conceptuel.Il est possible d'accéder aux métadonnées incluses dans des éléments d'annotation au moment de l'exécution en utilisant des classes de l'espace de noms System.Data.Metadata.Edm.

Exemple

L'exemple suivant illustre un élément EntityType avec un attribut d'annotation (CustomAttribute).L'exemple fait également apparaître un élément d'annotation appliqué à l'élément de type d'entité.Pour plus d'informations, consultez Éléments d'annotation (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>

Le code suivant récupère les métadonnées dans l'attribut d'annotation et les écrit dans la 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());
}

Le code ci-dessus suppose que le fichier School.csdl se trouve dans le répertoire de sortie du projet et que vous avez ajouté les instructions Imports et Using suivantes à votre projet :

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

Voir aussi

Concepts

Éléments d'annotation (CSDL)

Spécification CSDL

Autres ressources

Spécifications CSDL, SSDL et MSL