Reading Custom Annotation from $metadata URI for Open Data Service

Topics: General, OData Library, WCF Data Services
Aug 29, 2012 at 11:52 AM

Hello All,

I would like to quickly check if it is possible to read custom annotation at client side. If not are there any workarounds?

<Property xmlns:p8="http://schemas.microsoft.com/ado/2009/02/edm/annotation" 

Name="CategoryID" Type="Edm.Int32" Nullable="false"p8:StoreGeneratedPattern="Identity"/> 

For example, in above example, "Identity" is value of custom annotation.

Thanks in advance.

Viral Thakkar

Editor
Aug 29, 2012 at 5:24 PM

Hi,

Assuming you're using EdmLib to parse the $metadata, that is you get it as IEdmModel, you can use this extension method GetAnnotationValue to get the value of that parameter.

Thanks,

Vitek Karas

Aug 30, 2012 at 6:44 AM

 

Hi,

Thanks a lot for your reply.

I tried what you are saying but unfortunately that is not working.

I hope I am doing it correctly. To make sure that, sharing few more information.

I am hitting one of the public services. http://services.odata.org/Northwind/Northwind.svc/$metadata

I am getting edmmodel in following way:

IEdmModel edmmodel = messageReader.ReadMetadataDocument();                

and then

edmmodel.GetAnnotationValue((IEdmElement)edmmodel.SchemaElements.First(), "http://schemas.microsoft.com/ado/2009/02/edm/annotation", "StoreGeneratedPattern");

Above thing gives me null.

Please suggest.

Viral Thakkar

 

Editor
Sep 1, 2012 at 8:05 PM

Hi,

The problem is that you're asking the right question on a wrong element. edmmodel.SchemaElements.First() on the northwind model will return the Category EntityType. That itself doesn't have any annotation StoreGeneratedPattern.

Try this instead:

var annotation = model.GetAnnotationValue(
    ((IEdmEntityType)model.FindType("NorthwindModel.Category"))
        .FindProperty("CategoryID"),
    "http://schemas.microsoft.com/ado/2009/02/edm/annotation",
    "StoreGeneratedPattern");
Console.WriteLine(((IEdmStringConstantExpression)annotation).Value);

Thanks, 


Sep 3, 2012 at 1:28 PM

Yes. It is working now. Thanks a lot...