Annotations in CSDL

Published 11 January 08 12:39 PM | dpblogs 

 

The CSDL schema supports (what we internally call) annotations. Basically, annotations are XML attributes from a custom XML namespace that are part of your EntityType’s definition.

Here’s an example of an EntityType in CSDL with annotations. Notice that entity properties can have annotations too:

<EntityType

  Name="Customers"

  xmlns:attr="http://tempuri.org/MyCustomNamespace"

  attr:MyCustomAnnotation="ExampleAnnotation">

 

  <Key>

   <PropertyRef Name="CustomerID" />

  </Key>

 

  <Property Name="CustomerID" Type="String" Nullable="false"

   MaxLength="5" FixedLength="true"

   xmlns:attr="http://tempuri.org/MyCustomNamespace"

    attr:MyCustomAnnotation="ExampleAnnotation" />

 

  <!-- other properties -->

</EntityType>

 

A cursory glance at System.Data.Resources.CSDLSchema.xsd sitting in %vsinstalldir%\Xml\Schemas will tell you that pretty much all elements in CSDL allow attributes from a custom namespace. In fact, the <Schema /> element even allows elements from a custom XML namespace.

The real beauty of CSDL annotations is that the Entity Framework loads them into the metadata system when it loads the CSDL and you can access them as metadata properties via the public metadata APIs.

The following code snippet uses the metadata APIs to extract and dump CSDL annotations for the EntityType declared above:

using System;

using System.IO;

using System.Xml;

using System.Linq;

using System.Collections.Generic;

using System.Data.Metadata.Edm;

 

namespace ConsoleApplication1

{

  class Program

  {

    static void Main(string[] args)

    {

      string schema = @"

        <Schema Namespace='CNorthwind' Alias='Self'

          xmlns:cg='http://schemas.microsoft.com/ado/2006/04/codegeneration'

          xmlns:edm='http://schemas.microsoft.com/ado/2006/04/edm'

          xmlns='http://schemas.microsoft.com/ado/2006/04/edm'>

 

          <EntityType Name='Customer'

            xmlns:attr='http://tempuri.org/MyCustomNamespace'

            attr:MyCustomAnnotation='EntityAnnotation'>

 

            <Key>

              <PropertyRef Name='CustomerID' />

            </Key>

 

            <Property Name='CustomerID' Type='String' MaxLength='1024'

              Nullable='false'

              xmlns:attr='http://tempuri.org/MyCustomNamespace'

              attr:MyCustomAnnotation='PropertyAnnotation' />

 

            <Property Name='Address' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='City' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='CompanyName' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='ContactName' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='ContactTitle' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='Country' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='Fax' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='Phone' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='PostalCode' Type='String' MaxLength='1024'

              Nullable='false' />

            <Property Name='Region' Type='String' MaxLength='1024'

              Nullable='false' />

          </EntityType>

 

          <EntityContainer Name='NorthwindContext'>

            <EntitySet Name='Customers' EntityType='Self.Customer' />

          </EntityContainer>

        </Schema>";

 

      string myXmlns = "http://tempuri.org/MyCustomNamespace";

      EdmItemCollection collection = new EdmItemCollection(

        new XmlReader[]

        {

          XmlReader.Create(new StringReader(schema))

        }

      );

 

      foreach (EntityType entityType in collection.GetItems<EntityType>())

      {

        // Dump annotations on EntityType

        IEnumerable<MetadataProperty> entityAnnotations =

          entityType.MetadataProperties.Where(prop => prop.Name.StartsWith(myXmlns));

        foreach (MetadataProperty entityAnnotation in entityAnnotations)

        {

          Console.WriteLine("Entity: {0},\n\tAttribute: {1}\n\tValue: {2}",

            entityType.Name, entityAnnotation.Name, entityAnnotation.Value);

        }

 

        // Dump annotations on properties of EntityType

        foreach (EdmProperty entityProperty in entityType.Properties)

        {

          IEnumerable<MetadataProperty> propertyAnnotations =

            entityProperty.MetadataProperties.Where(prop => prop.Name.StartsWith(myXmlns));

          foreach (MetadataProperty propertyAnnotation in propertyAnnotations)

          {

            Console.WriteLine("Property {0},\n\tAttribute: {1}\n\tValue: {2}", entityProperty.Name, propertyAnnotation.Name, propertyAnnotation.Value);

          }

      }

 

      Console.ReadKey();

    }

  }

}

 

Btw, ADO.NET Data Services (code name Astoria) makes heavy use of CSDL annotations as described in their design notes.

 

Sanjay Nagamangalam
Program Manager, ADO.NET

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Abbas said on January 14, 2008 7:30 AM:

What is the release date of final verion?

# Roger Hendriks said on January 23, 2008 5:46 AM:

This is great, now we can extend the definition with our own business rules! I think we will go for EF instead of Linq2Sql :)

Will the annotations be supported by the designer....?

# Sanjay Nagamangalam (MSFT) said on January 24, 2008 1:11 PM:

Hi! Roger,

V1 of the designer won't have any user affordance to see and edit CSDL annotations. However, existing CSDL annotations in the EDMX file will be preserved by the designer. You can open the EDMX file in XML Editor to edit them.

I'll add more blog posts shortly that describe how to leverage CSDL annotations in the EDMX file within Visual Studio.

Regards,

Sanjay

# Marcelo's WebLog said on January 25, 2008 2:05 PM:

I admit it - sometimes I fall in love very hard with some of my tools. Tools that make me productive.

# ADO.NET team blog said on January 25, 2008 4:22 PM:

In previous posts, I’ve described CSDL annotations , how to extract CSDL from EDMX and introduced you

# DotNetKicks.com said on February 7, 2008 12:47 PM:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Leave a Comment

(required) 
(optional)
(required) 
Page view tracker