使用实体框架在Webapi可查询端点上扩展$ Expailing

时间:2018-07-19 16:37:03

标签: c# .net entity-framework asp.net-web-api asqueryable

我正在尝试在可查询的端点上进行$ expand并得到此错误。

Object of type 'System.Linq.EnumerableQuery`1[System.Web.Http.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand`1[pc_process_api.Models.Task]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[pc_process_api.Models.Task]'.

使用TaskComponent表将对象定义为多对多。 我的代码:

    [EnableQuery(MaxExpansionDepth = 5, PageSize = 10)]
            [Route("api/tasks")]
            public IQueryable<Task> GetTasks()
            {
                return db.Tasks.AsNoTracking().AsQueryable();
            }
public class Task 
    {
        public Task()
        {
            TaskComponents = new List<TaskComponent>();
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long TaskId { get; set; }
        [Required]
        public string Name { get; set; }

        public virtual ICollection<TaskComponent> TaskComponents { get; set; }
    }

 public class TaskComponent 
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long TaskComponentId { get; set; }

        [Required]
        public long SequenceNumber { get; set; }

        [MinLength(3, ErrorMessage = "Min Length is 3 chars"), MaxLength(50, ErrorMessage = "Max Length is 50 chars")]
        public string DisplayName { get; set; }

        [Required]
        public long TaskId { get; set; }
        public  Task Task { get;  set; }


        [Required]
        public long ComponentId { get; set; }
        public  Component Component { get;  set; }
    }

 public class Component
    {
        public Component()
        {           
            TaskComponents = new List<TaskComponent>();
        }
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long ComponentId { get; set; }
        [Required]
        public string Name { get; set; }

        public ICollection<TaskComponent> TaskComponents { get; set; }
        public string Code { get; set; }
    }

我尝试使用的网址是http://localhost:12346/api/tasks?$ expand = TaskComponents

请提出建议。

0 个答案:

没有答案