3

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.

4

2 回答 2

3

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);
于 2010-09-08T23:52:02.907 回答
1

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.

于 2010-09-11T00:47:06.170 回答