locked
LoadProperty works but expand don't! RRS feed

  • Question

  •  

    Hello,

     

    I m trying to do an eager loading of entities but I can't get it working.

     

    Code Snippet

    var result = (from formula in entities.FormulaEntitySet.Expand("ContextHierarchy,OperationHierarchy")

    where formula.Name == item.Name

    select formula).First();

     

     

    When I execute that, I get result.ContextHierarchy == null and also result.OperationHierarchy == null.

     

    If I do :

    Code Snippet

    entities.LoadProperty(result, "ContextHierarchy");

     

     

    then result.ContextHierarchy != null.

     

    I searched the forum and my connection string has MultipleActiveResultSets=True.

     

    I m pretty worried if can't do eager loading on direct properties I will never managed to load a Hierarchy of entities.

     

    Any help would be welcome

     

    Thank you by advance

     

     

    Tuesday, October 14, 2008 8:31 AM

Answers

  • Hi Gomata,

     

    Unfortunately, there is a hard coded limit of 8-levels on the depth of Expands because of the fact that the $expand queries are very expensive. The workaround would be to use LoadProperty beyond this level of depth.

     

    Thanks

    Waseem

    Wednesday, October 15, 2008 7:52 PM
    Answerer
  • Another option would be to set the DataServiceContext.MergeOptions property to PreserveChanges and then perform expand through manually created query URLs. For example to expand on territories for a particular employee:

     

    NorthwindEntities ctx = new NorthwindEntities(new Uri("http://localhost:34840/Northwind.svc/"));

    ctx.MergeOption = MergeOption.PreserveChanges;

    DataServiceQuery<Customers> customers = ctx.Customers.Expand("Orders/Employees");

    foreach (Customers c in customers)

    {

    Employees e = c.Orders[0].Employees;

    //ctx.LoadProperty(e, "Territories");

    StringBuilder b = new StringBuilder()

    .Append("Customers('")

    .Append(c.CustomerID)

    .Append("')/Orders(")

    .Append(c.Orders[0].OrderID)

    .Append(")/Employees")

    .Append("?$expand=Territories");

    var e2 = ctx.Execute<Employees>(new Uri(b.ToString(), UriKind.Relative));

    }

     

    Wednesday, October 15, 2008 10:58 PM
    Answerer

All replies

  • what is the uri that is generated by the Linq translator look like?  To see this call ToString() on the query object.

     

     

    Tuesday, October 14, 2008 6:16 PM
    Moderator
  • Hi Andrew thank you for your answer.

     

    This morning I did a console app to reproduce the issue but it was working! I can't explain why.

     

    The URI generated by the query was :

    http://10.168.0.92:8358/EntitiesService.svc/FormulaEntitySet()

    ?$filter=Name eq 'Prorata'&$expand=ContextHierarchy,OperationHierarchy

     

    So I tryied to go ahead and load the hierarchy

     

    I used

    Expand("ContextHierarchy/Children/Children/Children/Children/Children/Children/Children")

    and I reached the limite of 8, if I add more /Children I get an exception

    The specified $expand option is too deep. 

     

    I tried on entity framework side with the Include fonction but the same limite occured 

     

    is their a way to over come this limite?

     

    Thank you again.

     

     

     

     

    Wednesday, October 15, 2008 9:08 AM
  • Hi Gomata,

     

    Unfortunately, there is a hard coded limit of 8-levels on the depth of Expands because of the fact that the $expand queries are very expensive. The workaround would be to use LoadProperty beyond this level of depth.

     

    Thanks

    Waseem

    Wednesday, October 15, 2008 7:52 PM
    Answerer
  • Another option would be to set the DataServiceContext.MergeOptions property to PreserveChanges and then perform expand through manually created query URLs. For example to expand on territories for a particular employee:

     

    NorthwindEntities ctx = new NorthwindEntities(new Uri("http://localhost:34840/Northwind.svc/"));

    ctx.MergeOption = MergeOption.PreserveChanges;

    DataServiceQuery<Customers> customers = ctx.Customers.Expand("Orders/Employees");

    foreach (Customers c in customers)

    {

    Employees e = c.Orders[0].Employees;

    //ctx.LoadProperty(e, "Territories");

    StringBuilder b = new StringBuilder()

    .Append("Customers('")

    .Append(c.CustomerID)

    .Append("')/Orders(")

    .Append(c.Orders[0].OrderID)

    .Append(")/Employees")

    .Append("?$expand=Territories");

    var e2 = ctx.Execute<Employees>(new Uri(b.ToString(), UriKind.Relative));

    }

     

    Wednesday, October 15, 2008 10:58 PM
    Answerer
  • Thank you for the advises,

     

    I will try both an use the one that yield better results.

     

    Thursday, October 16, 2008 8:34 AM