WCF数据服务$ select返回“未实现”异常

时间:2012-01-20 19:33:44

标签: c#-4.0 entity-framework-4.1 wcf-data-services

我有一个我正在处理的项目,它有一个Order实体,它有一个OrderITems的导航属性:

public class Order
{
   public int OrderId { get; set; }
   public int CustomerId { get; set; }

   public virtual ICollection<OrderItem> OrderItems { get; set; }
}

public class OrderItem
{
   public int OrderId { get; set; }
   public int ProductId { get; set; }
   public int Quantity { get; set; }

   public virtual Order Order { get; set; }
}

如果我像这样查询OrderItem:

http://mysvc.com/mysvc/OrderItems?$expand=Order&$filter=ProductId eq 1234

它运作良好但我最终只需要绑定到Order。所以,我想像这样投影:

http://mysvc.com/mysvc/OrderItems?$expand=Order&$filter=ProductId eq 1234&$select=Order

但是,我收到了“未实施”的例外情况。

项目正在使用EntityFramework代码,首先是4.1数据提供程序,所有连线“显示”正确。我添加的唯一额外内容是路由生成干净的restful uris并通过IDispatchMessageInspector支持$ format选项。

好的,感谢Vitek的输入,我得到了一个更完整的例外:

<error>
    <code/>
    <message xml:lang="en-US">Not Implemented</message>
    <innererror>
        <message>Unable to create a constant value of type 'System.Data.Services.Internal.ProjectedWrapper1'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
        </message>
        <type>System.NotSupportedException</type>
        <stacktrace>   at System.Data.Objects.ELinq.ExpressionConverter.ConstantTranslator.TypedTranslate(ExpressionConverter parent, ConstantExpression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.ConditionalTranslator.TypedTranslate(ExpressionConverter parent, ConditionalExpression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input)

           at System.Data.Objects.ELinq.ExpressionConverter.TranslateLambda(LambdaExpression lambda, DbExpression input, DbExpressionBinding& binding)

           at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.OneLambdaTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, DbExpression& source, DbExpressionBinding& sourceBinding, DbExpression& lambda)

           at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SelectTranslator.Translate(ExpressionConverter parent, MethodCallExpression call)

           at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.SequenceMethodTranslator.Translate(ExpressionConverter parent, MethodCallExpression call, SequenceMethod sequenceMethod)

           at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.TypedTranslate(ExpressionConverter parent, MethodCallExpression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TypedTranslator`1.Translate(ExpressionConverter parent, Expression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.TranslateExpression(Expression linq)

           at System.Data.Objects.ELinq.ExpressionConverter.Convert()

           at System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)

           at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)

           at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()

           at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()

           at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.IEnumerable.GetEnumerator()

           at System.Data.Services.Internal.ProjectedWrapper.EnumerableWrapper.System.Collections.IEnumerable.GetEnumerator()

           at System.Data.Services.WebUtil.GetRequestEnumerator(IEnumerable enumerable)
           </stacktrace>
    </innererror>
</error>

1 个答案:

答案 0 :(得分:2)

如果这是使用EF Code First,则需要将服务定义为DataService并覆盖CreateDataSource以使用本文第5节中描述的技巧:http://social.technet.microsoft.com/wiki/contents/articles/5234.aspx

否则,WCF DS将DbContext视为反射提供程序,并运行用于LINQ to Objects的查询。这主要是有效的,但对于更复杂的查询,它有时会破坏。如果WCF DS知道它再次运行EF,它会为EF创建查询,这将始终有效。