views:

155

answers:

2

When I get the data using the URL in browser, I'm able to see the related data, for example:

http://localhost/services.svc/Dinners(1)/RSVPs

That lists 5 RSVPs for DinnerId = 1 in my case, but when I'm consuming the OData from a different project, I can only retrieve Dinners, debugging the app shows that RSVPs = 0, while it should be 5.

I'm consuming the service by adding a Service Reference to my project, and returning the data via a very simple LINQ query:

public ActionResult Index()
        {
            var result = (from d in db.Dinners
                          select d);

            return View(result);
        }

Any idea why d.RSVPs = 0 when it should be populated? I'm using EF (Code first - followed a post by ScottGu, with 2 very simple POCO classes for Dinner and RSVP. Dinner class have the collection of RSVPs: public ICollection<RSVP> RSVPs { get; set; }, and the RSVP class points to the Dinner with a foreign key public int DinnerId { get; set; } as well as the Dinner class: public Dinner Dinner { get; set; }.

Thanks.

+1  A: 

You need to use Expand() to access more than one level of the returned object graph.

Loading Deferred Content (WCF Data Services)

Something like:

var result = (from d in db.Dinners.Expand("RSVPs")
                      select d);
jfar
I was thinking about something similar after posted my question (Lazy loading?), but couldn't figure out the way. Using `Expand("RSVPs")` causes a different error: `Error processing response stream. Server failed with following message: An error occurred while processing this request.` Any idea why? Thank you very much.
Saxman
@Saxman, the string needs to be the property name. Please read the documentation and then try "RSVPs"
jfar
Hi jfar, that's what I did, I've used `"RSVPs"`, not `"RSVPS"`, also tried couple different combinations without success. :( Thanks.
Saxman
@Saxman, whats the full error, check the inner exception?
jfar
Inner exception is null, it gave me error on my asp.net mvc code though, where I have the foreach loop: `<% foreach (var d in Model) { %>` . Accessing the generated Url directly in browser also gives me the same error: `http://localhost:60538/service.svc/Dinners()?$expand=RSVPs`. Thanks for looking.
Saxman
Set UserVerboseErrors to true gave me a better error: `at System.Data.Services.Serializers.SyndicationSerializer.WriteObjectProperties(IExpandedResult expanded, Object customObject, ResourceType resourceType, Uri absoluteUri, String relativeUri, SyndicationItem item, DictionaryContent content, EpmSourcePathSegment currentSourceRoot) at System.Data.Services.Serializers.SyndicationSerializer.WriteEntryElement(IExpandedResult expanded, Object element, ResourceType expectedType, Uri absoluteUri, String relativeUri, SyndicationItem target)`
Saxman
A: 

Apparently, the problem is within the keyword virtual when I create the POCO classes with related tables. If I have the virtual keyword when referencing ICollection<RSVP> inside Dinner class or Dinner inside my RSVP class, lazy loading is enabled, but WCF Data Service stopped working! Taking the virtual keyword out and the WCF Data service started to work again, but then I don't have lazy/deferred loading enabled! I'm not sure if this is a feature or a bug?! For now, I guess I'll wait for the next release to really start using EF POCO and WCF Data Service.

Thanks all for looking.

Saxman