【问题标题】:WCF Data Services 5.0 + DbContext : How to return Related Entities?WCF 数据服务 5.0 + DbContext:如何返回相关实体?
【发布时间】:2012-07-24 22:47:51
【问题描述】:

我正在使用带有 EF DbContext 的 WCF 数据服务 5.0。

我看到旧文章说您可以使用 Expand 和 LoadProperty 让 WCF 在一个请求中返回相关实体。

我在 DbContext 中看到的唯一等价物是 Include,但这似乎不起作用。

在使用 DbContext 时有没有办法加载所有相关记录?

我正在使用以下代码(一个 CategoryGroup 可以包含多个 Category 实体):

[WebGet]
    public IQueryable<CategoryGroup> GetAllCategories(string activationCode)
    {
        try
        {
            var db = this.CurrentDataSource;

            var licence = db.Licenses
                .Include("Contact")
                .FirstOrDefault();

            if (licence != null)
            {
                var customerId = licence.Contact.CustomerId;

                return db.CategoryGroups.Include("Categories").Where(r => r.CustomerId == customerId);
            }
        }
        catch(Exception ex)
        {

        }

        return new List<CategoryGroup>().AsQueryable();
    }

这个函数只返回 OData 格式的 CategoryGroup 实体(没有相关的 Category 实体)。

【问题讨论】:

    标签: wcf entity-framework entity-framework-4 wcf-data-services odata


    【解决方案1】:

    目前 WCF DS 服务器无法自动扩展。因此,在上面的代码中包含 Include 对服务操作的响应没有影响。扩展只能由客户端通过 $expand 查询选项来请求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多