【问题标题】:WCF Data Services and EF 4 CTP 5 POCO - Unable to cast Unable to cast object of type '...DbQuery' to type 'System.Linq.IQueryable`WCF 数据服务和 EF 4 CTP 5 POCO - 无法转换 无法将“... DbQuery”类型的对象转换为“System.Linq.IQueryable”类型
【发布时间】:2011-01-31 18:37:38
【问题描述】:

我在查询表中的前 50 行时收到此错误消息。我的项目使用 Entity Framework 4 CTP 5 POCO:

Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery' to type 'System.Linq.IQueryable`1[Lib.Model.Post]'

我的模型基于这个答案:Entity Framework 4 CTP 4 / CTP 5 Generic Repository Pattern and Unit Testable

知道如何解决这个错误吗?

谢谢。

【问题讨论】:

  • 您需要提供更多上下文。就目前而言,我可以发挥惊人的克雷斯金并尝试创造所有可能性。围绕错误的代码是什么。

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


【解决方案1】:

更改 DataService 上下文并覆盖 ObjectContext 后,该服务现在可以工作了。以下是我所做的更改,以防有人也遇到同样的问题:

public class KennyService : DataService<MyDataContext>
{
    // Codes
}

public class KennyService : DataService<System.Data.Objects.ObjectContext>
{
    // Codes
}

protected override ObjectContext CreateDataSource()
{
    var context = ((IObjectContextAdapter)new Lib.MyDataContext()).ObjectContext;
    context.ContextOptions.ProxyCreationEnabled = false;

    return context;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2018-10-29
    • 2011-03-25
    相关资源
    最近更新 更多