locked
LS: Eager Loading from RIA Service? RRS feed

  • Question

  • In one of my Silverlight custom controls I am displaying a complex object which I would like to eager load. I have put a query in my RIA Service that assembles the object in its entirety (via  various .Include()s). Entity Framework is set to "Allow Lazy Load = False).

    The query does generate the correct results (all elements can be inspected), but the child elements appear not to get sent to the LS Client. Instead, LS fetches all child elements lazily when needed, which does work but is very slow.

    I cannot make use of static spans as they are not supported for queries from a RIA Service. Same thing for creating the query client-side: No ".Include()" allowed when using RIA Service. 

    Apart from accessing the data Service directly from the custom control, as Paul v. Bladel has described, is there no other way to eager load entities from a RIA Service?

    Thanks! 




    Friday, March 7, 2014 1:19 PM

Answers

  • Hi Jun Zh,

    thank you, I fully understand. I had just hoped that I might have overlooked something that would be obvious to more experiences eyes, but apparently not.

    I'll consider opening a support case.

    Thx again,

    bluewater 

    • Marked as answer by Angie Xu Monday, March 17, 2014 6:57 AM
    Friday, March 14, 2014 9:47 AM

All replies

  • Hi,

    >LS fetches all child elements lazily when needed, which does work but is very slow.

    Entity Framework supports three ways to load related data - eager loading, lazy loading and explicit loading. Please refer to the example below, and check whether other methods are available for you, I think it's useful to optimize your LS app.

    Loading Related Entities

    Best regards


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, March 10, 2014 6:04 AM
  • Angie,

    thank you for your reply - unfortunately the answers given in the link do not seem to solve my problem. I have done some more research in the meantime, so let me explain better what the issue is:

    1. I have a complex entity that has deeply-stacked child entities. Due to speed considerations, I need to eager load the parent object.
    2. The datasource that I am accessing is a RIA service that uses EF
    3. I am using, I believe, all required Includes in the RIA service ([Include] and .Include(...)). When I inspect the entity that my RIA query returns, all of the required child entities are there.
    4. If I .Load that very query from the lightswitch client side, none of the child entities seem to be there and therefore LS tries to lazy load the child entities (as I can determine from fiddler). For speed reasons this is not what I want - and why the heck are my child entities not there to begin with?
    5. If I work backwards from there and "fiddle" the response that the server sends, I can see that none of the child entities are returned, the odata does not even contain an entry for them.
    6. For analysis purposes, I called the service from the browser. The results were:

      a) if I use my custom query ("http://localhost:10000/MyService.svc/GetMyEntitySet?id=1"): The reply contains the child entities as links, but they are not expanded (despite the query having the .Include("MyChildEntitySet") part).
      b) Unsurprisingly, the same result occurs if I call the entity set, without any expand attribute (http://localhost:10000/MyService.svc/MyEntitySet(1))
      c) If I attach an $expand statement (http://localhost:10000/MyService.svc/MyEntitySet(1)?$expand=MyChildEntitySet) then I get the exception shown in the footer of this post. I interpret the exception as "I cannot cast RIAService.MyEntitySet to LightSwitchApplication.MyEntitySet". Not sure what that is trying to tell me ...
    7. If, just for testing purposes, I create a new datasource that calls my external database directly (no RIA service), then I am able to eager load the object as desired. So the problem must be in either Entity Framework or my RIA service.

    So I guess my real question is "why does the LS service not give me the child data that I know my RIA service provides?

    Thanks in advance,

    bluewater

    -------------------------------------------------------

    <m:message xml:lang="de-DE"><?xml version="1.0" encoding="utf-16"?><ExceptionInfo><Message>Unable to cast object of type 'System.Data.Services.Internal.ExpandedWrapper`2[WCF_RIA_Service.MyEntitySet,System.Collections.Generic.IEnumerable`1[WCF_RIA_Service.MyChildEntitySet]]' to type 'System.Data.Services.Internal.ExpandedWrapper`2[LightSwitchApplication.Implementation.MyEntitySet,System.Collections.Generic.IEnumerable`1[LightSwitchApplication.Implementation.MyChildEntitySet]]'.</Message><StackTrace> at System.Linq.Enumerable.&lt;CastIterator&gt;d__b1`1.MoveNext() at System.Linq.Enumerable.&lt;OfTypeIterator&gt;d__aa`1.MoveNext() at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.MergeQueryResults(IEnumerable results) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.&lt;&gt;c__DisplayClass28.&lt;Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.IDataProviderHost.MergeQueryResults&gt;b__27() at Microsoft.LightSwitch.Threading.DualDispatcherObject.Mutate(IDispatcher logicDispatcher, MutatorHost host, Action mutator) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceImplementation`1.Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.IDataProviderHost.MergeQueryResults(IEnumerable results) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.RiaDataProvider.DomainServiceQuery`1.Execute[TElement](IQueryable innerQuery, Expression outerExpression) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.OuterQuery`1.ExecuteInternal[TElement](IQueryable innerQuery, Expression outerExpression) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.OuterQueryExecutor`1.Execute[TElement](Expression expression) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataProvider.OuterQuery`1.GetEnumerator() at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceQueryProvider.QueryableWrapper`1.&lt;&gt;c__DisplayClass10.&lt;GetEnumerator&gt;b__f() at Microsoft.LightSwitch.Threading.DispatcherExtensions.Invoke(IDispatcher dispatcher, Action action) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataService`1.Microsoft.LightSwitch.ServerGenerated.Implementation.IODataService.LogicInvoke(Action a) at Microsoft.LightSwitch.ServerGenerated.Implementation.DataServiceQueryProvider.QueryableWrapper`1.GetEnumerator() at System.Data.Services.Providers.BasicExpandProvider.ExpandedQueryable`1.GetEnumerator() at System.Data.Services.Providers.BasicExpandProvider.ExpandedQueryable`1.System.Collections.IEnumerable.GetEnumerator() at System.Data.Services.WebUtil.GetRequestEnumerator(IEnumerable enumerable) at System.Data.Services.QueryResultInfo.MoveNext() at System.Data.Services.Providers.DataServiceExecutionProviderWrapper.GetSingleResultFromRequest(SegmentInfo segmentInfo) at System.Data.Services.DataService`1.CompareETagAndWriteResponse(RequestDescription description, IDataService dataService, IODataResponseMessage responseMessage) at System.Data.Services.DataService`1.SerializeResponseBody(RequestDescription description, IDataService dataService, IODataResponseMessage responseMessage) at System.Data.Services.DataService`1.HandleNonBatchRequest(RequestDescription description) at System.Data.Services.DataService`1.HandleRequest()</StackTrace><ErrorInfo /></ExceptionInfo></m:message>






    Monday, March 10, 2014 7:55 AM
  • Hi,

    From a support perspective this is really beyond what we can do here in the forums. If you cannot determine your answer here or on your own, consider opening a support case with us. Visit this link to see the various support options that are available to better meet your needs:  http://support.microsoft.com/default.aspx?id=fh;en-us;offerprophone.


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread. Regards, Jun Zh - MSFT Microsoft Online Community Support

    Friday, March 14, 2014 6:46 AM
  • Hi Jun Zh,

    thank you, I fully understand. I had just hoped that I might have overlooked something that would be obvious to more experiences eyes, but apparently not.

    I'll consider opening a support case.

    Thx again,

    bluewater 

    • Marked as answer by Angie Xu Monday, March 17, 2014 6:57 AM
    Friday, March 14, 2014 9:47 AM