Breeze API错误无法找到成员:在扩展实体上过滤时

时间:2013-06-21 15:48:50

标签: knockout.js odata breeze single-page-application

我收到错误:

Unable to locate member:

在扩展实体属性上进行过滤时。

这有效:

http://localhost:60760/XXX/XXX/productcollection?$filter=Code%20eq%20'da'&$expand=PRODUCT_LINE

并返回:

[{"$id":"1","$type":"DoorDesigner.Models.COLLECTION, DoorDesigner","ID":81,"Descr":"Some Description","Code":"Da","DISPLAY_HTML":"","DEFAULT_IMAGE_PATH":"../Content/Images/TempCollections/0000_Layer 54.jpg","PRODUCT_LINE":[{"$id":"2","$type":"DoorDesigner.Models.PRODUCT_LINE, DoorDesigner","ID":213,"CollectionID":81,"Code":"AT","Descr":"ATHENA","LongDescr":"ATHENA","DEFAULT_IMAGE_PATH":"../Content/Images/TempCollections/0000_Layer%2054.jpg","Product_Code_Group":"AT","COLLECTION":{"$ref":"1"},"PRICING_DOOR":[],"POSSIBLE_INSULATION":[],"POSSIBLE_WINDOW":[],"WINDOWs":[]},{"$id":"3","$type":"DoorDesigner.Models.PRODUCT_LINE, DoorDesigner","ID":217,"CollectionID":81,"Code":"CY","Descr":"CYPRUS","LongDescr":"CYPRUS","DEFAULT_IMAGE_PATH":"../Content/Images/TempCollections/Panel.jpg","Product_Code_Group":"CY","COLLECTION":{"$ref":"1"},"PRICING_DOOR":[],"POSSIBLE_INSULATION":[],"POSSIBLE_WINDOW":[],"WINDOWs":[]},{"$id":"4","$type":"DoorDesigner.Models.PRODUCT_LINE, DoorDesigner","ID":220,"CollectionID":81,"Code":"PI","Descr":"PINNACLE","LongDescr":"PINNACLE","DEFAULT_IMAGE_PATH":"../Content/Images/TempCollections/Windows.jpg","Product_Code_Group":"PI","COLLECTION":{"$ref":"1"},"PRICING_DOOR":[],"POSSIBLE_INSULATION":[],"POSSIBLE_WINDOW":[],"WINDOWs":[]},{"$id":"5","$type":"DoorDesigner.Models.PRODUCT_LINE, DoorDesigner","ID":227,"CollectionID":81,"Code":"WI","Descr":"WINDRIVER","LongDescr":"WINDRIVER","DEFAULT_IMAGE_PATH":"../Content/Images/TempCollections/Panel.jpg","Product_Code_Group":"WI","COLLECTION":{"$ref":"1"},"PRICING_DOOR":[],"POSSIBLE_INSULATION":[],"POSSIBLE_WINDOW":[],"WINDOWs":[]}]}]

请注意,PRODUCT_LINE已展开,并且有Descr属性。

现在,如果我更改要在PRODUCT_LINE.Descr上过滤的网址,我会收到错误消息:

http://localhost:60760/XXX/XXX/productcollection?$filter=PRODUCT_LINE/Descr%20eq%20'Athena'&$expand=PRODUCT_LINE 

返回:

{"$id":"1","$type":"System.Web.Http.HttpError, System.Web.Http","Message":"An error has occurred.","ExceptionMessage":"Unable to locate member: Descr","ExceptionType":"System.Exception","StackTrace":"   at Breeze.WebApi.ParseTreeVisitor.VisitMemberExpr(ParseTreeNode node, Expression targetExpr, String memberName)\r\n   at Breeze.WebApi.ParseTreeVisitor.<>c__DisplayClass3.<VisitNode>b__0(ParseTreeNode n)\r\n   at System.Collections.Generic.List`1.ForEach(Action`1 action)\r\n   at Breeze.WebApi.ParseTreeVisitor.VisitNode(ParseTreeNode node)\r\n   at Breeze.WebApi.ParseTreeVisitor.VisitNode(ParseTreeNode node)\r\n   at Breeze.WebApi.ParseTreeVisitor.Parse(Type rootType, ParseTreeNode node)\r\n   at Breeze.WebApi.ExpressionTreeBuilder.Parse(Type rootType, String source)\r\n   at Breeze.WebApi.ODataActionFilter.BuildFilterFunc(String filterQueryString, Type elementType)\r\n   at Breeze.WebApi.ODataActionFilter.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)"}

据我所知,这是产生错误的Breeze代码。

protected virtual Expression VisitMemberExpr(ParseTreeNode node, Expression targetExpr, String memberName) {
  var targetType = targetExpr.Type;
  var member = targetType.GetMember(memberName).FirstOrDefault();
  if (member == null) {
    throw new Exception("Unable to locate member: " + memberName);
  }
  return Expression.MakeMemberAccess(targetExpr, member);
}

这种情况在我的API中发生,我尝试在扩展属性上进行过滤。有没有人成功过滤这些?

更新: 这是实体查询:

breeze.EntityQuery.from('ProductCollection') .where('PRODUCT_LINE.Descr', '==', 'Athena') .expand('PRODUCT_LINE');

AND在APIController中

[HttpGet] public IQueryable<COLLECTION> productCollection() { return _contextProvider.Context.COLLECTIONs; }

上下文定义:

public DbSet<COLLECTION> COLLECTIONs { get; set; }

最后是类定义

public partial class COLLECTION
{
    public COLLECTION()
    {
        this.PRODUCT_LINE = new HashSet<PRODUCT_LINE>();
    }

    public int ID { get; set; }
    public string Descr { get; set; }
    public string Code { get; set; }
    public string DISPLAY_HTML { get; set; }
    public Nullable<System.DateTime> CREATED { get; set; }
    public string CREATOR { get; set; }
    public Nullable<System.DateTime> LAST_MODIFIED { get; set; }
    public string LAST_USER { get; set; }
    public string DEFAULT_IMAGE_PATH { get; set; }
    public Nullable<bool> IS_DELETED { get; set; }

    public virtual ICollection<PRODUCT_LINE> PRODUCT_LINE { get; set; }
}

public partial class PRODUCT_LINE
{
    public PRODUCT_LINE()
    {
        this.PRICING_DOOR = new HashSet<PRICING_DOOR>();
        this.POSSIBLE_INSULATION = new HashSet<POSSIBLE_INSULATION>();
        this.POSSIBLE_WINDOW = new HashSet<POSSIBLE_WINDOW>();
        this.WINDOWs = new HashSet<WINDOW>();
    }

    public int ID { get; set; }
    public Nullable<int> CollectionID { get; set; }
    public string Code { get; set; }
    public string Descr { get; set; }
    public string LongDescr { get; set; }
    public string DISPLAY_HTML { get; set; }
    public Nullable<System.DateTime> CREATED { get; set; }
    public string CREATOR { get; set; }
    public Nullable<System.DateTime> LAST_MODIFIED { get; set; }
    public string LAST_USER { get; set; }
    public string DEFAULT_IMAGE_PATH { get; set; }
    public Nullable<bool> IS_DELETED { get; set; }
    public string Product_Code_Group { get; set; }
    public Nullable<int> DefaultInsulationId { get; set; }
    public Nullable<int> DefaultWindowId { get; set; }

    public virtual COLLECTION COLLECTION { get; set; }
    public virtual ICollection<PRICING_DOOR> PRICING_DOOR { get; set; }
    public virtual ICollection<POSSIBLE_INSULATION> POSSIBLE_INSULATION { get; set; }
    public virtual ICollection<POSSIBLE_WINDOW> POSSIBLE_WINDOW { get; set; }
    public virtual ICollection<WINDOW> WINDOWs { get; set; }
}

谢谢,

2 个答案:

答案 0 :(得分:1)

对我来说没有意义的一件事是你的ProductCollection是Product的集合还是ProductCollection类型的实体?

修改

在你的ApiController中,你将ProductCollection命名为productCollection,是故意的还是错误的?此外,您的PRODUCT_LINE系列名为PRODUCT_LINE,人们认为您的意思是将其命名为PRODUCT_LINES。最后,您的ForeignKey可以为空(CollectionId),您确定它不为空吗?

答案 1 :(得分:0)

我能够解决此错误。不知怎的,我有一个Breeze 1.1.2的引用,它没有显示它在Package Manager中有任何更新。我终于意识到,如果我搜索微风,它发现v 1.3.6是当前版本(这是我认为项目无论如何)。

当前版本的微风更新解决了这个问题。

kadumel =&gt;感谢您的宝贵意见!