OData WCF 数据服务 - 调用服务时相关数据(通过外键)不会显示
当我在浏览器中使用 URL 获取数据时,我可以看到相关数据,例如:
http://localhost/services.svc/Dinners(1)/RSVPs
在我的例子中,列出了 DiningId = 1 的 5 个 RSVP,但是当我使用来自不同项目的 OData 时,我可以只检索晚餐,调试应用程序显示 RSVP = 0,而它应该是 5。
我通过向我的项目添加服务引用来使用该服务,并通过一个非常简单的 LINQ 查询返回数据:
public ActionResult Index()
{
var result = (from d in db.Dinners
select d);
return View(result);
}
知道为什么 d。 RSVPs = 0 什么时候应该填充?我正在使用 EF(首先是代码 - 接下来是 ScottGu 的帖子,其中有 2 个非常简单的用于晚餐和 RSVP 的 POCO 类。晚餐类有 RSVP 的集合: public ICollection
public intdinnerId { get; }
以及 Dining 类:publicdinnerdinner{get;谢谢
。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要使用 Expand() 来访问返回的对象图的多个级别。
加载延迟内容(WCF 数据服务)
类似:
You need to use Expand() to access more than one level of the returned object graph.
Loading Deferred Content (WCF Data Services)
Something like:
显然,当我使用相关表创建 POCO 类时,问题出在关键字
virtual
内。如果我在引用 Dining 类中的ICollection
或 RSVP 类中的Dinner
时使用virtual
关键字,则会启用延迟加载,但 WCF数据服务停止工作!去掉virtual
关键字后,WCF 数据服务再次开始工作,但我没有启用延迟/延迟加载!我不确定这是一个功能还是一个错误?!现在,我想我会等待下一个版本才能真正开始使用 EF POCO 和 WCF 数据服务。感谢大家的观看。
Apparently, the problem is within the keyword
virtual
when I create the POCO classes with related tables. If I have thevirtual
keyword when referencingICollection<RSVP>
inside Dinner class orDinner
inside my RSVP class, lazy loading is enabled, but WCF Data Service stopped working! Taking thevirtual
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.