简体   繁体   English

使用带有WebAPI和ViewModel的ODATA $ expand查询选项

[英]Using the ODATA $expand query option with WebAPI and a ViewModel

This question is very similar but does not give me what I need. 这个问题非常相似,但没有给我我需要的东西。

I am using Entity Framework 6. My database has two tables, Customers and CustomerTypes. 我正在使用Entity Framework 6.我的数据库有两个表,Customers和CustomerTypes。 I have created a ViewModel for each. 我为每个人创建了一个ViewModel。 A customer can have a type: 客户可以拥有以下类型:

public class Customer
{
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    public CustomerTypeViewModel CustomerType { get; set; }
}

public class CustomerTypeViewModel
{
    public int CustomerTypeID { get; set; }
    public string CustomerTypeDescription { get; set; }
}

I have a Customer controller which exposes an odata action method with a return type of IQueryable: 我有一个Customer控制器,它公开了一个返回类型为IQueryable的odata动作方法:

[HttpPost, Queryable(AllowedQueryOptions = AllowedQueryOptions.All)]
    public IQueryable<CustomerViewModel> GetCustomersMatchingCriteria([FromBody]ODataActionParameters parameters)
    {
        var criteria = (CustomerMassUpdateCriteriaViewModel)parameters["Criteria"];

        return Common.Customers.GetCustomerMassUpdateCriteriaResults(criteria,
            ConfigurationManager.AppSettings["CLIENT_ID"]).Select(
            c => new CustomerViewModel()
            {
                CustomerID = c.CustomerID,
                CustomerName = c.CustomerName,
                CustomerType = new CustomerTypeViewModel() 
                {
                    CustomerTypeDescription = c.CustomerType.CustomerTypeDescription
                }
            });
    }

The Common.Customers.GetCustomerMassUpdateCriteriaResults method just returns an IQueryable of Customer, which is the actual entity. Common.Customers.GetCustomerMassUpdateCriteriaResults方法只返回Customer的IQueryable,它是实际的实体。

The problem is, when calling this controller method with the following query string options: 问题是,当使用以下查询字符串选项调用此控制器方法时:

$expand=CustomerType
$select=CustomerID,CustomerName,CustomerType/CustomerTypeDescription

This exception is thrown: 抛出此异常:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; 'ObjectContent`1'类型无法序列化内容类型'application / json的响应主体; charset=utf-8'.","type":"System.InvalidOperationException" 字符集= UTF-8' “”类型“:” System.InvalidOperationException”

The argument to DbIsNullExpression must refer to a primitive, enumeration or reference type. DbIsNullExpression的参数必须引用基元,枚举或引用类型。

Removing the $expand option and the associated CustomerType/CustomerTypeDescription property from the $select list produces no error. 从$ select列表中删除$ expand选项和关联的CustomerType / CustomerTypeDescription属性不会产生任何错误。

I feel like I'm missing something obvious, here. 在这里,我觉得我错过了一些明显的东西。 Any ideas? 有任何想法吗?

1st EDIT: 第一次编辑:

Enumerating the results via the ToList() extension method and returning IEnumerable rather than IQueryable successfully expands the CustomerType navigation property, but my ODATA $select list is no longer respected at the database level. 通过ToList()扩展方法枚举结果并返回IEnumerable而不是IQueryable成功扩展了CustomerType导航属性,但我的ODATA $选择列表在数据库级别不再受到尊重。 Doesn't that defeat the purpose of using ODATA? 这不是打败了使用ODATA的目的吗?

Exception : 例外:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; 'ObjectContent`1'类型无法序列化内容类型'application / json的响应主体; charset=utf-8'.","type":"System.InvalidOperationException" 字符集= UTF-8' “” 类型 “:” System.InvalidOperationException”

per my knowledge is due to how request is reaching for OData formatting. 根据我的知识,这是由于请求如何达到OData格式。 The request should come to OData route for formatting. 请求应该到OData路由进行格式化。 If you could move GlobalConfiguration.Configuration.EnableOData() before RouteConfig.RegisterRoutes and WebApiConfig.Register in global.asax could help. 如果你可以在global.asax中的RouteConfig.RegisterRoutesWebApiConfig.Register之前移动GlobalConfiguration.Configuration.EnableOData()可以提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM