1

当我尝试在任何导航属性上使用 Expand 时,出现以下异常。

    $id: "1",
    $type: "System.Web.Http.HttpError, System.Web.Http",
    Message: "An error has occurred.",
    ExceptionMessage: "'object' does not contain a definition for 'Include'",
    ExceptionType: "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException",
    StackTrace: " at System.Web.Http.ApiController.<InvokeActionWithExceptionFilters>d__b.MoveNext() --- End of stack trace from previous  location where exception was thrown --- at  System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 

如果我将包含在 Breeze 控制器中,一切正常。如果我不这样做并且只是在客户端上使用扩展,我会收到该错误。任何帮助是极大的赞赏!

这是用于尝试检索数据的 URL

   /breeze/maxsys/CallOutcomes?$expand=CallOutcomeAction

这是模型

[Table("CallOutcomes")]
public class CallOutcome {
    [Key]
    public int Id { get; set; }

    [Required]
    public string Description { get; set; }
    public bool IsInternal { get; set; }


    public int CallOutcomeActionId { get; set; }

    [ForeignKey("CallOutcomeActionId")]
    [InverseProperty("CallOutcomes")]
    public CallOutcomeAction CallOutcomeAction { get; set; }
    public ICollection<CallOutcomeHistory> CallOutcomeHistories { get; set; }
}

控制器如下所示(我删除了其他一些 get 方法)

[BreezeController]
[Authorize]
[RequireHttps]
public class MaxsysController : ApiController

    protected IMaxsysBreezeRepository Repository { get; set; }

    public MaxsysController(IMaxsysBreezeRepository repository)
    {
        Repository = repository;
    }

    [HttpGet]
    public IQueryable<CallOutcome> CallOutcomes()
    {
        return Repository.CallOutcomes;
    } 
}

错误来自 BreezeQueryableAttribute.cs 中的此方法

    public virtual IQueryable ApplyExpand(IQueryable queryable, string expandsQueryString, HttpRequestMessage request)
    {
        (from s in expandsQueryString.Split(new char[] { ',' }) select s.Trim()).ToList<string>().ForEach(delegate (string expand) {
            queryable = (IQueryable) ((dynamic) queryable).Include(expand.Replace('/', '.'));
        });
        return queryable;
    }

参数值为

   queryable = {SELECT 
[Extent1].[Id] AS [Id], 
N'b1d28373-98a2-4a88-9733-7872acd28bd2' AS [C1], 
[Extent1].[Description] AS [Description], 
[Extent1].[IsInternal] AS [IsInternal], 
[Extent1].[CallOutcomeActionId] AS [CallOutcomeActionId], 
N'CallOutcomeAction' AS [C2], 
N'b1d28373-98a2-4a88-9733-7872acd28bd2' AS [C3], 
[Extent2].[Id] AS [Id1], 
[Extent2].[Description] AS [Description1]
FROM  [dbo].[CallOutcomes] AS [Extent1]
INNER JOIN [dbo].[CallOutcomeActions] AS [Extent2] ON [Extent1].[CallOutcomeActionId] = [Extent2].[Id]}

expandsQueryString = "CallOutcomeAction"

    HttpRequestMessage ={Method: GET, RequestUri: 'http://127.0.0.1:82/breeze/maxsys/CallOutcomes?$expand=CallOutcomeAction', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Connection: keep-alive
  Accept: text/html
  Accept: application/xhtml+xml
  Accept: application/xml; q=0.9
  Accept: */*; q=0.8
  Accept-Encoding: gzip
  Accept-Encoding: deflate
  Accept-Encoding: sdch
  Accept-Language: en-US
  Accept-Language: en; q=0.8
  Host: 127.0.0.1:81
  User-Agent: Mozilla/5.0
  User-Agent: (Windows NT 6.1; WOW64)
  User-Agent: AppleWebKit/537.36
  User-Agent: (KHTML, like Gecko)
  User-Agent: Chrome/27.0.1453.94
  User-Agent: Safari/537.36
}}
4

4 回答 4

2

问题解决了。安装了一些预发布软件来测试 Web API 中的 CORS 支持。一旦我删除了这些更改,expand 就可以正常工作了。谢谢您的帮助。

于 2013-06-05T15:37:56.527 回答
1

我也遇到过这个问题,并将其追溯到 BreezeQueryable 注释的应用程序。

例外:

ExceptionMessage: "'System.Linq.EnumerableQuery' 不包含 'Include' 的定义",

端点:

[HttpGet]
[BreezeQueryable(PageSize = 1000)] //1000 row limit
public IQueryable<Postcode> Postcodes()
{
    return _db.Context.Postcodes;
}

调用脚本:

var qry = breeze.EntityQuery
                .from('Postcodes')
                .where('Name', breeze.FilterQueryOp.StartsWith, searchTerm)
                .orderBy('PostcodeId')
                .expand('State')
                .take(25);

要修复,只需删除 [BreezeQueryable] 注释:

[HttpGet]
//[BreezeQueryable(PageSize = 1000)] //1000 row limit
public IQueryable<Postcode> Postcodes()
{
    return _db.Context.Postcodes;
}

编辑 - 显然这不是“固定的”。看起来像是 Breeze WebApi 程序集中的问题。

于 2013-09-18T11:24:40.010 回答
0

预赛

您确定可以在不展开MaxsysController.CallOutcomes的情况下查询端点吗?

您确定Repository.CallOutcomes返回正确的 EF 具体类型吗?

您在控制器内部尝试了类似以下的操作CallOutcomes()

[HttpGet]
    公共 IQueryable CallOutcomes()
    {
        var foo = Repository.CallOutcomes // 在这里中断;稍后删除。
                  .Include('CallOutcomeAction').ToList();
        返回 Repository.CallOutcomes;
    }

在休息时,您已经确认该方法成功而没有失败并且foo具有价值。

下一个

一旦你确认了这些观点,我想我的下一步就是删除[RequireHTTPS]. 我最近没有尝试过,而且对我来说要快速设置它并不容易。有可能我们搞砸了,[RequireHTTPS]属性和[BreezeController]属性之间存在干扰。

也尝试把[RequireHTTPS]之前[BreezeController]。没关系;我现在只是猜测。我会等待你的报告,然后我们可以从那里继续前进。

于 2013-06-04T20:56:14.510 回答
0

当我尝试使用操作上的 BreezeQueryableAttribute 限制操作返回的页面大小时,我开始遇到此问题。如果没有该属性,该操作将返回预期的数据。像 gopheruk 一样,我相信 Breeze 目前存在一个错误。

库版本:

没有使用任何预发布库,并且 nuget 中的所有内容都是最新的。

  • Breeze 1.4.2(Breeze.WebApi 1.4.0.0、Breeze.WebApi.EF 1.4.0.0)
  • Microsoft.AspNet.WebApi 4.0.30506
  • 实体框架 5.0.0

请求:

http://localhost/breeze/Data/MyObjects?$filter=Id%20eq%201&$expand=User

在服务器上:

[BreezeController]
public class DataController : ApiController
{

    //...

    [HttpGet]
    [BreezeQueryable(PageSize = 30)] //if this line is commented out, everything works
    public IQueryable<MyObject> MyObjects()
    {
        return _myObjectRepository.All(User.Identity);
    }

}

响应:

{"$id":"1","$type":"System.Web.Http.HttpError, System.Web.Http","Message":"An error has occurred.","ExceptionMessage":"'System.Linq.EnumerableQuery<MyObject>' does not contain a definition for 'Include'","ExceptionType":"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException","StackTrace":"   at CallSite.Target(Closure , CallSite , Object , String )\r\n   at System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1)\r\n   at Breeze.WebApi.QueryHelper.<>c__DisplayClass14.<ApplyExpand>b__11(String expand)\r\n   at System.Collections.Generic.List`1.ForEach(Action`1 action)\r\n   at Breeze.WebApi.QueryHelper.ApplyExpand(IQueryable queryable, String expandsQueryString)\r\n   at Breeze.WebApi.QueryHelper.ApplySelectAndExpand(IQueryable queryable, NameValueCollection map)\r\n   at Breeze.WebApi.BreezeQueryableAttribute.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.CallOnActionExecuted(HttpActionContext actionContext, HttpResponseMessage response, Exception exception)\r\n   at System.Web.Http.Filters.ActionFilterAttribute.<>c__DisplayClass2.<System.Web.Http.Filters.IActionFilter.ExecuteActionFilterAsync>b__0(HttpResponseMessage response)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass41`2.<Then>b__40(Task`1 t)\r\n   at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)"}
于 2013-09-30T09:52:24.930 回答