扩展拥有类型 OData EF Core 的导航属性

问题描述 投票:0回答:1

我有以下实体:

[Table("tblA")]
public class EntityA
{
    [Key]
    public int ID { get; set; }

    [Required]
    public virtual EntityB EntityB { get; set; }
}

[Owned]
public class EntityB
{   
    public string Name { get; set; }

    public string Description { get; set; }

    public virtual EntityD EntityD { get; set; }
}

[Table("tblD")]
public class EntityD
{
    [Key]
    public int ID { get; set; }

    public int Count { get; set; }
}

我想使用 OData 检索 EntityA(包括 EntityD)。我尝试了以下请求,但它不起作用:https://localhost:7249/EntityA?$expand=EntityD 并返回错误消息:

"message": "The query specified in the URI is not valid. Could not find a property named 'EntityD' on type 'EntityA'.",
c# entity-framework asp.net-core entity-framework-core odata
1个回答
0
投票

你还需要展开两次,我尝试如下:

https://localhost:xxxx/odata/EntityA?$expand=EntityB($expand=EntityD)

注册服务:

var modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntityType<EntityD>();
modelBuilder.EntityType<EntityB>();
modelBuilder.EntitySet<EntityA>("EntityA");

builder.Services.AddControllers().AddOData(
    options => options.Select().Filter().OrderBy().Expand().Count().SetMaxTop(null).AddRouteComponents(
        "odata",
        modelBuilder.GetEdmModel())); 

结果:

© www.soinside.com 2019 - 2024. All rights reserved.