1

我将 GridView 绑定到以下 EntityDataSource:

<asp:EntityDataSource ID="sessionQuery" runat="server" 
    ConnectionString="name=encoreEntities" DefaultContainerName="encoreEntities"
    EnableFlattening="False" EntitySetName="sessions" Include="user, user.person" 
    Where="it.lastactivity > @NowMinusXMins" OrderBy="it.lastactivity desc" >   
    <WhereParameters>
        <asp:Parameter Name="NowMinusXMins" Type="DateTime" />
    </WhereParameters>
</asp:EntityDataSource>

这工作正常。

我有另一个名为 SessionHistory 的表,它有一个进入 Session 的外键。此表跟踪会话期间访问的页面。我想将 Session 实体链接到 SessionHistory 中的最新记录,并在 GridView 中显示页面。

加入将是这样的:

select * from session s where s.sessionid = (select top 1 sessionid from sessionhistory order by lastactivitydate desc)

我可以像在这篇文章中那样做:EntityDataSource query inner join 但是,我试图保留 EF 关系,以便我可以在我的 Gridview 中引用它们(例如 session.user.username、session.user.person.lastname、 ETC。)

有没有办法做到这一点?

4

1 回答 1

0

这是一个真实的示例,其中有两个具有三个导航属性的表。

  • lm_m_location_type表定义一个位置(国家、城市、州等)
  • lm_m_location表包含按父/子层次结构排列的位置行(例如 USA --> Florida --> Tampa, Orlando, Miami )

关系

  • lm_m_location_type定义零对多位置
  • lm_m_location可以有零对多的子位置
  • lm_m_location可以有零个或一个父位置

导航属性(来自 EntityFramework 通过 Edit XML)

  • NavigationProperty 名称= ChildLocations关系=DB.fk_child_loc FromRole=lm_m_location ToRole=lm_m_location1

  • NavigationProperty 名称= ParentLocation关系=DB.fk_parent_loc FromRole=lm_m_location1 ToRole=lm_m_location

  • NavigationProperty 名称= LocationType关系=DB.fk_loc_type FromRole=lm_m_location ToRole=lm_m_location_type

代码示例:用于访问位置并包括父行、子行和位置类型行。

//
// code from the controller
//

public ActionResult getLocation()
{
    var lm_m_location = db.lm_m_location
        .Include("ParentLocation")
        .Include("ChildLocations")
        .Include("LocationType")
        .Where(t => t.parent_location_id == LinkDB.MvcApplication.Location.Current.id);

    return View(lm_m_location.ToList());
}

//
// code from the view that accesses each of the included tables
//
@model IEnumerable<LinkDBuilder.Models.lm_m_location>

@Html.DisplayFor(modelItem => item.description)
@Html.DisplayFor(modelItem => item.ParentLocation.description)
@Html.DisplayFor(modelItem => item.LocationType.description)

    // inside a loop of the child locations
    @Html.DisplayFor(modelItem => item.ChildLocations.description)

    // just to show we could drill back to the grand parent location if we wanted
    // of course we would make sure there was a value first in production code
    @Html.DisplayFor(modelItem => item.ParentLocation.ParentLocation.description)

.

于 2013-05-28T20:46:04.480 回答