Atributos de anotación (CSDL)

En el lenguaje de definición de esquemas conceptuales (CSDL), los atributos de anotación son atributos XML personalizados del modelo conceptual.Además de tener una estructura XML válida, los atributos de anotación deben cumplir las condiciones siguientes:

  • Los atributos de anotación no deben estar en ningún espacio de nombres XML reservado para CSDL.

  • Se pueden aplicar varios atributos de anotación a un elemento CSDL determinado.

  • Dos atributos de anotación cualesquiera no pueden tener el mismo nombre completo.

Los atributos de anotación se pueden usar para proporcionar metadatos adicionales sobre los elementos en un modelo conceptual.Es posible obtener acceso en tiempo de ejecución a los metadatos incluidos en los elementos de anotación usando clases del espacio de nombres System.Data.Metadata.Edm.

Ejemplo

En el ejemplo siguiente se muestra un elemento EntityType con un atributo Annotation (CustomAttribute).El ejemplo también muestra un elemento Annotation aplicado al elemento de tipo de entidad.Para obtener más información, vea Annotation (Elementos) (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>

El siguiente código recupera los metadatos del atributo Annotation y los escribe en la consola:

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());
}

El código anterior supone que el archivo School.csdl está en el directorio de resultados del proyecto y que se han agregado las siguientes instrucciones Imports y Using al proyecto:

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

Vea también

Conceptos

Annotation (Elementos) (CSDL)

Especificación CSDL

Otros recursos

Especificaciones CSDL, SSDL y MSL