问题
I have 3 levels of hierarchy in my data:
DepartmentList -> EmployeeCollection -> Employee
Basically, there are a number departments, each containing a number of employees.
Here is my source code:
public class DataService : DataService<Departments>
public class Departments
{
private List<Department> _deptCollection;
public IQueryable<Department> DeptCollection { get { return this._deptCollection.AsQueryable(); } }
...
...
}
[DataServiceKey("DepartmentId")]
public class Department
{
public string DepartmentId { get; set; }
private IList<EmployeeBase> _employees { get; set; }
public IQueryable<EmployeeBase> Employees
{
get { return _employees.AsQueryable(); }
}
...
}
[DataServiceKey("Id")]
public class EmployeeBase
{
public string Id { get; set; }
public string Name { get; set; }
}
When I try to browse the DataService, I get the following error:
The server encountered an error processing the request. The exception message is 'On data context type 'Departments', there is a top IQueryable property 'DeptCollection' whose element type is not an entity type. Make sure that the IQueryable property is of entity type or specify the IgnoreProperties attribute on the data context type to ignore this property.'. See server logs for more details. The exception stack trace is:
at System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(IDictionary
2 knownTypes, IDictionary
2 childTypes, IDictionary2 entitySets) at System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() at System.Data.Services.DataService
1.CreateProvider() at System.Data.Services.DataService1.HandleRequest() at System.Data.Services.DataService
1.ProcessRequestForMessage(Stream messageBody) at SyncInvokeProcessRequestForMessage(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
I think I need to implement the relationship navigation here, which an entity data model does by itself for a SQL Source. But I'm not too sure. Any pointers would be very helpful. Please let me know if you need any further information.
Thanks.
回答1:
The key property on an entity class must be a property. In your case the Department.DepartmentId is a field. Turn it into a property. One other note. There's no need to return IQueryable from the Department.Employees, only IEnumerable will be used anyway (it doesn't hurt though). You will need an IQueryable property on your Departments class for this to work. Each entity must have its own top-level entity set.
来源:https://stackoverflow.com/questions/5572525/relationship-navigation-in-wcf-data-service-with-custom-object-data-source