Can I use a complex datatype with an EF-provided W

2019-08-06 00:52发布

问题:

I am using an Entity Framework-provided WCF DataService to expose an OData endpoint. I want to create a Service Operation that returns a new type. But I get:

The exception message is 'Unable to load metadata for return type 'System.Linq.IQueryable1[MyNamespace+MyNewType]' of method 'System.Linq.IQueryable1[MyNamespace+MyNewType] FlightHours()'.'. See server logs for more details.

How can I let the DataService know I want it to grok MyNewType? Do I have to hackily make a fake entity on the EF DataContext, or can I simply "add" it in somehow? This post came up with that hack but I was hoping things had changed or that they missed something.

Update with relevant links:

  • I assume WCF DataService with Entity Framework: TimeSpan support would be solved the same way.
  • This MSDN discussion implies that methods like this exist for Reflection and Custom sources but make no mention of EF.
  • Jun 2013 update: Thanks to @dgorissen who found this possible counterexample: WCF Data Services 5.0 Workaround for returning POCOs? I haven't had a chance to test this myself, and I don't know why it would work, but maybe it will...

回答1:

There's currently no other way than to teach the EF that such a type exists. When you use the EF provider for WCF DS, the entire metadata comes solely from EF, WCF DS doesn't modify it in any (meaningful) way.