【发布时间】:2011-03-22 16:58:52
【问题描述】:
我在从服务操作返回我认为是 IQueryable 的内容时遇到了强制转换问题,但显然是 DbQuery。
更新:我的一些代码(ServiceOp,返回 IEnumerable)基于 Scott Hanselman 的 post here:
[WebGet]
public IQueryable<Post> GetPopularPosts()
我已经设置了一个基本的 WCF 数据服务。根据update bug,我将其定义为:
public class Api : DataService<ObjectContext>
而不是来自 TestDb。我将 CreateDataSource() 设置为:
protected override ObjectContext CreateDataSource()
{
var testDb = new TestDb();
var objectContext = ((IObjectContextAdapter)testDb).ObjectContext;
objectContext.ContextOptions.ProxyCreationEnabled = false;
return objectContext;
}
在我的 InitializeService 方法中,我调用:
config.SetServiceOperationAccessRule("GetStuff", ServiceOperationRights.AllRead);
而我的服务操作是:
[WebGet]
public IQueryable<Stuff> GetStuff()
{
var context = new TestDb();
return context.Stuff;
}
问题在于,即使方法完成,并且 context.Stuff 中有项目,服务器也会返回错误:
An error occurred while processing this request. Exception has been thrown by the target of an invocation. System.Reflection.TargetInvocationException
...
Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery' to type 'System.Linq.IQueryable`
对此有什么想法吗?
【问题讨论】:
-
你能在这里添加上下文代码吗? Stuff 和 Context 中的其他对象有什么关系吗?
标签: c# wcf linq entity-framework wcf-data-services