【问题标题】:Dynamically Adding Entity Sets to ODataConventionModelBuilder or ODataModelBuilder将实体集动态添加到 ODataConventionModelBuilder 或 ODataModelBuilder
【发布时间】:2018-02-16 23:42:07
【问题描述】:

有没有办法将 EntitySets 动态添加到 ODataConventionModelBuilder。

我正在 .net 中开发 OData 服务。我们将返回的一些实体来自外部程序集。我很好地阅读了程序集并获得了相关类型,但由于这些类型是变量,我不确定如何将它们定义为实体集。

例子:

    public static void Register(HttpConfiguration config)
    {
        //some config house keeping here

        config.MapODataServiceRoute("odata", null, GetEdmModel(), new DefaultODataBatchHandler(GlobalConfiguration.DefaultServer));

        //more config housekeeping
    }

    private static IEdmModel GetEdmModel()
    {
        ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
        builder.Namespace = "SomeService";
        builder.ContainerName = "DefaultContainer";

        //These are the easy, available, in-house types
        builder.EntitySet<Dog>("Dogs"); 
        builder.EntitySet<Cat>("Cats");
        builder.EntitySet<Horse>("Horses"); 

        // Schema manager gets the rest of the relevant types from reading an assembly.  I have them, now I just need to create entity sets for them

        foreach (Type t in SchemaManager.GetEntityTypes)
        {
            builder.AddEntityType(t); //Great!  but what if I want EntitySET ?
            builder.Function(t.Name).Returns<IQueryable>();  //See if you can put correct IQueryable<Type> here.

            //OR

            builder.EntitySet<t>(t.Name);  //exception due to using variable as type, even though variable IS a type

        }

        return builder.GetEdmModel();
    }

【问题讨论】:

    标签: asp.net-web-api odata


    【解决方案1】:

    想通了。只需在循环中添加这一行:

    builder.AddEntitySet(t.Name, builder.AddEntityType(t));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-10
      • 2015-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多