Reputation: 338
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. I have created a ViewModel for each. 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:
[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.
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; charset=utf-8'.","type":"System.InvalidOperationException"
The argument to DbIsNullExpression must refer to a primitive, enumeration or reference type.
Removing the $expand option and the associated CustomerType/CustomerTypeDescription property from the $select list produces no error.
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. Doesn't that defeat the purpose of using ODATA?
Upvotes: 5
Views: 2823
Reputation: 136
Exception :
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'.","type":"System.InvalidOperationException"
per my knowledge is due to how request is reaching for OData formatting. The request should come to OData route for formatting. If you could move GlobalConfiguration.Configuration.EnableOData() before RouteConfig.RegisterRoutes and WebApiConfig.Register in global.asax could help.
Upvotes: 0