Search code examples
c#asp.netodataasp.net-web-api2data-annotations

Adding annotations to the EDM in OData v4 in ASP.NET Web Api 2.2


I have an entity, Foo, with a property, Bar (an Edm.String, say).

Suppose that I want to add an annotation to this - let's call it "ReadOnly" (or if that has some special meaning in OData v4, choose any custom annotation - maybe "Blurgum"). It's an Edm.Boolean (or whatever, "Edm.String" if Boolean isn't a real thing).

How do I get this annotation to show up in the entity metadata? Note that I'm letting the service generate the metadata and using ODataModelBuilder construct the EDM for me from a Foo C# class.

It seems like this should be easy, but everything I've investigated has been a dead end:

  • model has a member with methods like "SetAnnotationValue" and "SetAnnotationValues", but I can't find a way to get the IEdmElement corresponding to the Foo/Bar property (namespace is FooService, FWIW).
  • it looks like in previous versions of OData ASP.NET, you could supply your own custom metadata file, but I don't see a way to do that now
  • I've seen some examples doing stuff with custom "serializers", but don't know enough to know whether these are worth pursuing. If I want to add annotations to properties, types, etc., is this the only way?

I'd like to see the annotation in the returned metadata; I understand this might require sending some preference headers in the request, but we'll cross that bridge when we come to it.


Solution

  • Now these annotations are not supported. Here is a related bug: https://aspnetwebstack.codeplex.com/workitem/1451