如何在基本类型上创建 WCF 数据服务
我正在尝试做这样的事情:
public class DataService : DataService<EmployeeCollection>
{
public IQueryable<EmployeeBase> GetEmployeeData(string EmpIdSubstring)
{
var employees = new List<EmployeeBase>();
employees.Add(new Level1Employee());
employees.Add(new Level2Employee());
return employees.AsQueryable();
}
}
这里:
Level1Employee
和Level2Employee
类派生自EmployeeBase
类。EmployeeBase
类定义用[DataServiceKey("Id")]
修饰。EmployeeCollection
包含一个IEnumerable
。
但是当我在浏览器中浏览此服务时,出现以下错误:
内部服务器错误。类型“Level1Employee”不是复杂类型或实体类型。
我尝试将 [DataServiceKey]
属性从 EmployeeBase
类移动到 Level1Employee
和 Level2Employee
类定义。但遇到同样的错误。
请注意,如果我返回包含 EmployeeBase
类型元素的员工集合,则该服务工作正常。
有可能实现这一目标吗?非常感谢任何帮助/指示/见解。
I am trying to do something like this:
public class DataService : DataService<EmployeeCollection>
{
public IQueryable<EmployeeBase> GetEmployeeData(string EmpIdSubstring)
{
var employees = new List<EmployeeBase>();
employees.Add(new Level1Employee());
employees.Add(new Level2Employee());
return employees.AsQueryable();
}
}
Here:
Level1Employee
andLevel2Employee
classes are derived fromEmployeeBase
class.EmployeeBase
class definition is decorated with[DataServiceKey("Id")]
.EmployeeCollection
contains anIEnumerable<EmployeeBase>
.
But when I browse this service in the browser, I get the following error:
Internal Server Error. The type 'Level1Employee' is not a complex type or an entity type.
I tried moving the [DataServiceKey]
attribute from EmployeeBase
class to Level1Employee
and Level2Employee
class definitions. But getting the same error.
Note that the service works fine if I returns an employees collection containing elements of type EmployeeBase
.
Is it possible to achieve this? Any help/ pointers/ insights are much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种基于 RIA 服务的 DomainServices 行为的盲目猜测。
尝试为每个子类添加一个函数。各返回
IQueryable
和IQueryable。
This is kind of a shot in the dark based upon the behavior of DomainServices from RIA services.
Try adding a function for each subclass. One each to return
IQueryable<Level1Employee>
andIQueryable<Level2Employee>.
上下文类(在您的情况下为 EmployeeCollection)必须公开一个带有 getter 和 IQueryable 类型的公共属性,并且 EmployeeBase 类必须具有 DataServiceKey 属性,以便 EmployeeBase (及其所有派生类型)被识别为实体类型。
您还需要在方法上添加 [WebGet] 属性,以便将其识别为服务操作。
The context class (EmployeeCollection in your case) must expose a public property with a getter and a type of IQueryable and the EmployeeBase class must have the DataServiceKey attribute in order for EmployeeBase (and all its derived types) to be recognizes as entity types.
You also need to add [WebGet] attribute on your method for it to be recognized as service operation.