代码之家  ›  专栏  ›  技术社区  ›  johnny 5

OData元数据公开所有实体。净核心

  •  0
  • johnny 5  · 技术社区  · 6 年前

    我试图将Odata配置为仅公开上下文中的某些实体。我正在使用。净核心。使用7.0.0 Beta2。包裹

    services.AddOData();
    
    //...
    
    app.UseMvc(routeBuilder =>
    {
        routeBuilder.MapODataServiceRoute("odata", null, GetModel());
        routeBuilder.EnableDependencyInjection();
    });
    
    public static IEdmModel GetModel()
    {
        var builder = new ODataConventionModelBuilder();
        var skillSet = builder.EntitySet<Skill>(nameof(Skill));
        skillSet.EntityType.Count().Filter().OrderBy().Expand().Select();
        builder.ContainerName = "DefaultContainer";
    
        return builder.GetEdmModel();
    }
    

    当我导航到$元数据页面时,我可以看到我公开的实体以及数据库上下文中的所有其他实体

    <?xml version="1.0" encoding="utf-8"?>
    <edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
        <edmx:DataServices>
            <Schema Namespace="Entities.Models" xmlns="http://docs.oasis-open.org/odata/ns/edm">
                <EntityType Name="Skill">
                    <Key>
                        <PropertyRef Name="Id" />
                    </Key>
                    <Property Name="Id" Type="Edm.Int32" Nullable="false" />
                    <Property Name="Name" Type="Edm.String" />
                    <NavigationProperty Name="RequestNegotiations" Type="Collection(Entities.Models.RequestNegotiation)" />
                    <NavigationProperty Name="UserSkills" Type="Collection(Entities.Models.UserSkill)" />
                </EntityType>
                <EntityType Name="Conversation">
                    <Key>
                        <PropertyRef Name="Id" />
                    </Key>
                    <Property Name="Id" Type="Edm.Int32" Nullable="false" />
                    <Property Name="Created" Type="Edm.DateTimeOffset" Nullable="false" />
                    <NavigationProperty Name="ConversationSubscriptions" Type="Collection(Entities.Models.ConversationSubscription)" />
                    <NavigationProperty Name="Messages" Type="Collection(Entities.Models.Message)" />
                    <NavigationProperty Name="ServiceRequests" Type="Collection(Entities.Models.ServiceRequest)" />
                </EntityType>
    

    如何仅公开在启动中注册的实体?

    1 回复  |  直到 6 年前
        1
  •  0
  •   johnny 5    6 年前

    原来这是因为我的导航属性。如果您公开导航属性,它将检查所有连接的导航属性

    public static IEdmModel GetModel()
    {
        var builder = new ODataConventionModelBuilder();
        var skillSet = builder.EntitySet<Skill>(nameof(Skill));
        skillSet.EntityType.Ignore(x => x.RequestNegotiations);
        skillSet.EntityType.Ignore(x => x.UserSkills);
    
        builder.ContainerName = "DefaultContainer";
    
        return builder.GetEdmModel();
    }