使用 EF 4 和 RIA 服务连接两个表并返回整个结果

发布于 2024-10-13 07:52:39 字数 456 浏览 3 评论 0 原文

我正在使用 RIA 服务和实体框架 4.0 构建 Silverlight 应用程序。我的 SQL 数据库中有两个表,我想将它们连接起来并作为单个结果返回。首先,我的两个表:


Room:
id
RoomName
BuildingId

Building:
id
BuildingName

我的默认域服务生成了默认的 GetRooms 方法:

Public Function GetRooms() As IQueryable(Of Room)
    Return Me.ObjectContext.Rooms
End Function

但这似乎不包含有关我的建筑物的详细信息,即使 Room.BuildingId 和 Building.Id 之间存在引用完整性。有人可以指出我正确的方向吗?

I'm building a Silverlight application using RIA services and the entity framework 4.0. I have two tables in my SQL database that I would like to join and return as a single result. First, my two tables:


Room:
id
RoomName
BuildingId

Building:
id
BuildingName

My Default domain service generated a default GetRooms method:

Public Function GetRooms() As IQueryable(Of Room)
    Return Me.ObjectContext.Rooms
End Function

But this doesn't appear to contain details about my buildings even though there is referencial integrity between Room.BuildingId and Building.Id. Can someone please point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静 2024-10-20 07:52:39

为了优化性能,默认情况下查询中不包含相关表。您需要使用 [Include] 标记要包含在查询中的表。此外,您需要在查询中包含该表。 Tim Heuer 阅读了一篇有关如何执行此操作的博文:

http://timheuer.com/blog/archive/2010/01/05/master-details-with-ria-services-and-includedresults.aspx

To optimice performance related tables are not included by default in a query. You need to mark the tables that you wish to include in your query with [Include]. Furthermore you need to include the table in your query. Tim Heuer have reading a blogpost about how to do it:

http://timheuer.com/blog/archive/2010/01/05/master-details-with-ria-services-and-includedresults.aspx

寒尘 2024-10-20 07:52:39

如果您希望包含建筑物,则需要在代码中使用 .Include("Building")

Public Function GetRooms() As IQueryable(Of Room)
    Return Me.ObjectContext.Rooms.Include("Building")
End Function

If you want the buildings to be included, you need to use the .Include("Building") to your code:

Public Function GetRooms() As IQueryable(Of Room)
    Return Me.ObjectContext.Rooms.Include("Building")
End Function
恰似旧人归 2024-10-20 07:52:39

根据您的要求,您可能需要考虑返回“表示模型”对象的集合(每个对象都包含有关房间和建筑物的信息),而不是实体。您在服务器项目中定义一个类,并使用域操作中实体模型中的数据(传递到客户端)填充该类型的对象集合。有关执行此操作的更多信息如下:http://msdn。 microsoft.com/en-us/library/ee707347(VS.91).aspx

Depending upon your requirements, you might want to consider returning a collection of "presentation model" objects (with each object containing information about both the room and the building), rather than entities. You define a class in the server project, and populate a collection of objects of this type with the data from the entity model in a domain operation (which is passed to the client). More information on doing so is here: http://msdn.microsoft.com/en-us/library/ee707347(VS.91).aspx.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文