1

I'm building an OData service with Web API. Database access is handled via Entity Framework.

I am mapping my EF entities to another set of classes using AutoMapper. Those mapped classes are what is being exposed via the OData service.

I have a model that looks like this:

Assignment(int AssignmentId, int EmployeeId, Employee Employee)
Employee(int EmployeeId, ICollection<Skill> Skills)
Skill(int SkillId, string SkillName)

My OData endpoint exposes an IQueryable<Assignment>. Everything works fine with simple OData queries ($top, $select, $filter, etc). By default, Assignment.Employee is not returned from the service; I am fine with that.

When I attempt to $expand the Employee, I get this error:

{"error":{"code":"","message":"An error has occurred.","innererror":{"message":"Cannot compare 'member 'Skills' of type '[...].Employee''. Only primitive types, enumeration types and entity types are supported.","type":"System.NotSupportedException"[...]}

[...] are not actually part of the error, just pieces of the message I've removed.

At this point I have not requested Skills, and my expectation was that Employee.Skills would not be expanded because I haven't explicitly requested to expand it. I am unsure what Skills is being compared to that is throwing EF off.

The best I can tell is that OData is applying an additional projection on top of any projections I have applied, and it is that projection that EF is having issues with.

Has anyone had experience/success with using OData/EF to navigate properties that had more than one level of depth to it?

I have tried removing AutoMapper and writing the expressions by hand, but I still run into the same error, so I don't believe AutoMapper is causing any issues here.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
Brian Ball
  • 12,268
  • 3
  • 40
  • 51
  • Can you please update your question with the actual uri that your are using to call the service, that should help us to narrow down the possible causes of this. The compare error suggests that you have a filter somewhere, it may not be in your uri, it might be introduced in your code. Update with these missing infos and I'll explain more in an answer – Chris Schaller Mar 23 '17 at 22:05
  • Silly question, pre-empting the another common reason, have you applied the EnableQuery attribute to your endpoint? If there is not a lot of code in it, posting your method that is processing the request would complete the story. – Chris Schaller Mar 23 '17 at 22:09
  • I do have EnableQueryAttribute on my ODataController. I'm going to start from scratch and write a solution as simple as possible that reproduces the problem, then I'll make it available and hopefully that will help make my problem more clear (in addition to adding the URL to the question). I'll update it once this is done. – Brian Ball Mar 27 '17 at 12:46
  • Also showing the actual url parameters sent will be of value to us punters at home :) So we can confirm on our services, and maybe meet you in the middle with a simple demo of our own for you to try – Chris Schaller Mar 27 '17 at 15:54
  • I have to recreate the problem to know exactly which URL is causing the error. I moved in a different direction and can't easily reproduce it right now. I believe the error happened when the query string was as simple as `?$expand=Skills`, but even that worked fine initially. The error only started when I added a second navigation property to the entity or the navigation property itself that I was expanding had a navigation property to a third entity. – Brian Ball Mar 27 '17 at 18:30

0 Answers0