How to prevent ODataConventionModelBuilder to automatically expose all derived types' metadata?

↘锁芯ラ 提交于 2019-12-01 03:39:56

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!