【发布时间】:2019-12-12 23:12:50
【问题描述】:
我为移动应用程序准备了 Odata API,但我遇到了 $Expand 的问题。当我使用此查询时,一切正常。
查询: .../odata/forms?$expand=FormFields($expand=FormFieldRadios)&$filter=FormId eq 1
但是当我使用这个查询时:
.../odata/forms(1)?$expand=FormFields($expand=FormFieldRadios)
Expand 不起作用,并且项目集合 si empy。
我的 json 结果 Items coolection One item
配置服务:
services.AddOData();
services.AddODataQueryFilter();
services.AddMvc(options =>
{
// https://blogs.msdn.microsoft.com/webdev/2018/08/27/asp-net-core-2-2-0-preview1-endpoint-routing/
options.EnableEndpointRouting = false;
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
配置
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Expand().Filter().Count().OrderBy().MaxTop(250).Expand(QueryOptionSetting.Allowed);
routeBuilder.MapODataServiceRoute("odata", "odata", GetEdmModel(app.ApplicationServices));
});
模型构建器
builder.EntitySet<Form>("Forms").EntityType.Filter().Count().Expand(OdataDefaults.MaxExpansionDepth).OrderBy().Page().Select();
表单实体
public class Form
{
[Key, Required]
public int FormId { get; set; }
[Required]
public int Order { get; set; }
[Required]
public bool Active { get; set; }
[Required, MaxLength(255)]
public string Name { get; set; }
public int IsDeleted { get; set; }
[MaxLength(255)]
public string SuccessMessage { get; set; }
public ICollection<FormField> FormFields { get; set; } = new List<FormField>();
}
Odata 控制器
public class FormsController : ODataController
{
private VillageContext _db;
public FormsController(VillageContext context)
{
_db = context;
}
[EnableQuery(AllowedFunctions = OdataDefaults.AllowedFunctions, MaxExpansionDepth = OdataDefaults.MaxExpansionDepth, PageSize = OdataDefaults.PageSize)]
public IActionResult Get()
{
return Ok(_db.Forms);
}
[EnableQuery(AllowedFunctions = OdataDefaults.AllowedFunctions, MaxExpansionDepth = OdataDefaults.MaxExpansionDepth, PageSize = OdataDefaults.PageSize)]
public IActionResult Get([FromODataUri] int key)
{
return Ok(_db.Forms.Find(key));
}
}
【问题讨论】:
标签: .net asp.net-core entity-framework-core odata