I'm using ODataConventionModelBuilder to build Edm Model for Web API OData Service like this:
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 public class Q derived from Z located in different assembly.
The ODataConventionModelBuilder will generates Edm Model that includes definition of class Q (among other derived classes) and it will be exposed with service metadata. That is undesirable in our case.
When derived class in inaccessible (e.g. defined as internal) such problem, sure, doesn't exist.
Is there way to force the ODataConventionModelBuilder to do NOT automatically expose all derived types' metadata?
This should work:
ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
builder.Namespace = "X";
builder.ContainerName = "Y";
builder.EntitySet("Z");
builder.Ignore<Q>();
IEdmModel edmModel = builder.GetEdmModel();
There is no way of disabling the automatic discovery and this is by design. See here.
However, there's a workaround. You have to explicitly Ignore each derived type and then proceed on manually mapping each derived type. Here's a nice loop to ignore the 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With