WCF 数据服务中与自定义(对象)数据源的关系导航
我的数据有 3 个层次结构:
部门列表->员工集合 ->员工
基本上,有许多部门,每个部门包含许多员工。
这是我的源代码:
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; }
}
当我尝试浏览 DataService 时,出现以下错误:
服务器遇到错误 处理请求。例外情况 消息是“关于数据上下文类型” ‘部门’,有一个顶部 IQueryable 属性“DeptCollection” 其元素类型不是实体 类型。确保 IQueryable 属性是实体类型或指定 IgnoreProperties 属性 数据上下文类型忽略此 财产。'。查看服务器日志了解更多信息 细节。异常堆栈跟踪是:
在 System.Data.Services.Providers.ReflectionServiceProvider.PopulateMetadata(IDictionary
2 knownTypes,IDictionary
2 个子类型, IDictionary2 个实体集)位于 System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata() 在 System.Data.Services.DataService
1.CreateProvider() 在 System.Data.Services.DataService1.HandleRequest() 在 System.Data.Services.DataService
1.ProcessRequestForMessage(流 消息正文)位于 SyncInvokeProcessRequestForMessage(对象 , 对象[] , 对象[] ) 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象 实例,Object[] 输入,Object[]& 输出)在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& 远程过程调用)在 System.ServiceModel.Dispatcher.MessageRpc.Process(布尔值 isOperationContextSet)
我想我需要在这里实现关系导航,这是实体数据模型为 SQL 源自行完成的。但我不太确定。任何指示都会非常有帮助。 如果您需要任何进一步的信息,请告诉我。
谢谢。
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(IDictionary2
2 childTypes,
knownTypes, IDictionary
IDictionary2 entitySets) at
1.CreateProvider()
System.Data.Services.Providers.BaseServiceProvider.PopulateMetadata()
at
System.Data.Services.DataService
at
System.Data.Services.DataService1.HandleRequest()
1.ProcessRequestForMessage(Stream
at
System.Data.Services.DataService
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实体类的关键属性必须是属性。在您的情况下,Department.DepartmentId 是一个字段。将其变成财产。
另一张纸条。没有必要从 Department.Employees 返回 IQueryable,无论如何只会使用 IEnumerable (不过这并没有什么坏处)。
您的 Departments 类需要一个 IQueryable 属性才能使其工作。每个实体必须有自己的顶级实体集。
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.