【问题标题】:How can I return IQueyrable DTO from Webapi Get so I can use Odata filters如何从 Webapi Get 返回 IQueyrable DTO,以便我可以使用 Odata 过滤器
【发布时间】:2012-07-30 11:08:27
【问题描述】:

我正在尝试在 Web api 项目 ASP.NET MVC 4.0 RC 中使用 ODP.net 和实体框架的 Odata 过滤器。我想返回一个 IQueryable 的 OwnDTO 。我收到一个内部 500 错误,没有任何详细信息。我知道 webapi RC 存在错误生成错误,但我不认为该错误是我的问题。

    Get http://localhost:51744/api/Owner  called using Fiddler




    [Queryable]
    public IQueryable<OwnDTO> Get()        
    {            
        using (Entities context = new Entities())
        {
            var query = from item in context.Owners
                        select
                        new OwnDTO
                        {
                            Name = item.Name

                        };
            return query.AsQueryable();
        }
    }

//非常简单的例子

 public class OwnDTO
    {
        public string Name;
    }

我不想使用我的 Oracle EF 生成的类 (DAO) 从我的 Get 返回,但我知道如果我用更友好的界面替换 EntityObject 我可以。如果我返回 IEnumerable 它可以工作,但我想要 Odata 过滤器。


如果有人想要一个工作示例,请更新。应该在 linq 中使用 Automapper 或类似的,并且应该注入上下文。


    [Queryable]
    public IQueryable<OwnDTO> Get()        
    {            
        {
            var query = from item in Hack._EFContext.Owners
                        select
                        new OwnDTO
                        {
                            Name = item.Name

                        };
            return query.AsQueryable();
        }
    }

效果很好,但看起来 Odata 在 RC 后已被删除。所以我需要寻找另一条路。

【问题讨论】:

    标签: entity-framework-4 asp.net-mvc-4 asp.net-web-api odata odp.net


    【解决方案1】:

    它确实可以在 RC 中使用,但在发布时可能不适用于 RTM - 还不是很清楚。

    您的问题是您正在处理您的上下文,因为您使用的是 using 块。所以上下文在数据被检索之前就被处理掉了。

    所以不是using,而是在请求结束时注册您的对象以进行处置。 Tugberk 有一个帖子here

    【讨论】:

    • [Queryable] 属性似乎已从 RTM 中消失。它在哪里?
    猜你喜欢
    • 2021-10-08
    • 1970-01-01
    • 1970-01-01
    • 2016-02-12
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多