如何在基本类型上创建 WCF 数据服务

发布于 2024-10-28 19:16:03 字数 1039 浏览 8 评论 0原文

我正在尝试做这样的事情:

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();
  }
}

这里:

  1. Level1EmployeeLevel2Employee 类派生自 EmployeeBase 类。
  2. EmployeeBase 类定义用 [DataServiceKey("Id")] 修饰。
  3. EmployeeCollection 包含一个 IEnumerable

但是当我在浏览器中浏览此服务时,出现以下错误:

内部服务器错误。类型“Level1Employee”不是复杂类型或实体类型。

我尝试将 [DataServiceKey] 属性从 EmployeeBase 类移动到 Level1EmployeeLevel2Employee 类定义。但遇到同样的错误。

请注意,如果我返回包含 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:

  1. Level1Employee and Level2Employee classes are derived from EmployeeBase class.
  2. EmployeeBase class definition is decorated with [DataServiceKey("Id")].
  3. EmployeeCollection contains an IEnumerable<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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雪若未夕 2024-11-04 19:16:03

这是一种基于 RIA 服务的 DomainServices 行为的盲目猜测。

尝试为每个子类添加一个函数。各返回 IQueryableIQueryable

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> and IQueryable<Level2Employee>.

a√萤火虫的光℡ 2024-11-04 19:16:03

上下文类(在您的情况下为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文