How to prevent ODataConventionModelBuilder to automatically display metadata of all derived types?

I use ODataConventionModelBuilder to create the Edm Model for Web API OData Service as follows:

ODataModelBuilder builder = new ODataConventionModelBuilder(); builder.Namespace = "X"; builder.ContainerName = "Y"; builder.EntitySet<Z>("Z"); IEdmModel edmModel = builder.GetEdmModel(); 

Class Z is located in one assembly and there is a public class Q derived from Z located in another assembly.

ODataConventionModelBuilder generates an Edm Model that includes a Q class definition (among other derived classes), and it will be displayed with service metadata. This is undesirable in our case.

When a derived class is not available (for example, defined as internal), such a problem, of course, does not exist.

Is there a way to make ODataConventionModelBuilder NOT automatically show metadata of all derived types?

+8
c # odata asp.net-web-api2
source share
2 answers

This should work:

 ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); builder.Namespace = "X"; builder.ContainerName = "Y"; builder.EntitySet("Z"); builder.Ignore<Q>(); IEdmModel edmModel = builder.GetEdmModel(); 
+8
source share

It is not possible to turn off automatic detection, and this is by design. See here .

However, there is a workaround. You must explicitly ignore each derived type, and then continue to manually match each derived type. Here's a nice loop to ignore derived types:

 var builder = new ODataConventionModelBuilder(); builder.Namespace = "X"; builder.ContainerName = "Y"; builder.EntitySet<Z>("Z"); var types = AppDomain.CurrentDomain.GetAssemblies() .SelectMany(a => a.GetTypes()) .Where(t => t.IsSubclassOf(typeof(Z))); foreach (var type in types) builder.Ignore(types.ToArray()); //additional mapping of derived types if needed here var edmModel = builder.GetEdmModel(); 

See my blog post for more details.

+4
source share

All Articles