【问题标题】:$Expand does not work when i get one item当我得到一件物品时,$Expand 不起作用
【发布时间】: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

My nugets

配置服务:

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


    【解决方案1】:

    您是否针对您的实体(表单)定义了一个键?

    查看演示 http://services.odata.org/V4/TripPinService/$metadata - 您会看到定义了一个键,因此您可以通过您选择的键引用实体成员。 示例:

      <EntityType Name="Person" OpenType="true">
       <Key>
           <PropertyRef Name="UserName" />
       </Key>
    

    然后根据OData examples,您可以使用如下查询: GET serviceRoot/People('russellwhyte')

    【讨论】:

      【解决方案2】:

      EntityFramework 的Find 方法不返回IQueryable&lt;TEntity&gt;,而是返回TEntity 本身。因此,OData 不能再对其进行扩展是合乎逻辑的,因为不再有要“修改”的表达式树。 将您的功能更改为:

      [EnableQuery(AllowedFunctions = OdataDefaults.AllowedFunctions, MaxExpansionDepth = OdataDefaults.MaxExpansionDepth, PageSize = OdataDefaults.PageSize)]
      public IActionResult Get([FromODataUri] int key)
      {
          return Ok(SingleResult.Create(_db.Forms.Where(form => form.FormId == key)));
      }
      

      【讨论】:

      • 感谢您的想法,但如果我使用此函数,则会出现问题,因为函数获取数组。我需要获取一项的功能
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 2021-12-15
      • 1970-01-01
      • 1970-01-01
      • 2018-10-30
      相关资源
      最近更新 更多