是否可以将传入的OData请求转换为其他模型/实体集?

时间:2019-06-17 14:27:10

标签: c# asp.net odata

我有一个OData请求,我试图将其转换为两个不同的模型,具体取决于过滤器字符串是否包含特定值。但是,当我尝试使用其他模型创建新的ODataQueryOption并将其传递到控制器时,会遇到连续错误。

目前,我的班级是这样的

        [HttpGet]
        [Route("RepairRequisitions/GetRepairRequisitions")]
        public HttpResponseMessage GetRepairRequisitions(ODataQueryOptions<RepairRequitisionDTO> options)
        {
           //return new HttpResponseMessage(statusCode: HttpStatusCode.Created);

            string uri = options.Request.RequestUri.AbsoluteUri;
            if (uri.Contains("RepairId"))
            {
                var queryName = options.Request.GetQueryNameValuePairs();
                var filterValue = queryName.Where(x => x.Key == "$filter").Select(y => y.Value).FirstOrDefault();

                if (filterValue == null || !filterValue.Split(new string[] { "eq", "and" }, StringSplitOptions.RemoveEmptyEntries)[1].ToLower().Trim().StartsWith("'rms"))
                {

                    ODataModelBuilder modelBuilder = new ODataModelBuilder();
                    modelBuilder.EntitySet<Repair>("Repair");
                    var repairpath = $"{options.Request.RequestUri.Scheme}://{options.Request.RequestUri.Authority}/api/Repair/GetRepairs";
                    var repairrequest = options.Request.RequestUri.Query.Replace("%27", "");
                    var repairUri = new Uri(repairpath + repairrequest);

                    var newRequest = new HttpRequestMessage(HttpMethod.Get, repairUri);
                    var content = new ODataQueryContext(modelBuilder.GetEdmModel(), typeof(Repair));
                    var repairOptions = new ODataQueryOptions<Repair>(content, newRequest);
                    var newOdataQueryOptions = (ODataQueryOptions<Repair>)Activator.CreateInstance(repairOptions.GetType(), repairOptions.Context, repairOptions.Request);

                    var repairResult = _repairController.GetRepairs(newOdataQueryOptions); 
                    return repairResult;

                }
            }



        }

这确实起作用,因为它正确地调用了GetRepairs类,并传入了新的Odata查询,但是,当它尝试对数据库运行它时,我总是得到的错误消息是:“无法在类型“ repair”上找到名为“ RepairId”的属性”

Repair确实具有哪种类型!我无法理解是什么导致了此错误。也许是我如何调用新的ODataQueryOptions?任何帮助将不胜感激!

0 个答案:

没有答案