EntityDataSource从GridView的另一个实体访问一个集合 [英] EntityDataSource accessing a collection from another entity in GridView

查看:205
本文介绍了EntityDataSource从GridView的另一个实体访问一个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如果我的标题准确地描述我想要做的。

I'm not sure if my title accurately describes what I'm trying to do.

我有我的实体框架,人员和用户两个表。我想,以显示用户的网格,所以我成立了这个EntityDatasource:

I have two tables in my Entity Framework, person and user. I am trying to display a grid of users, so I set up this EntityDatasource:

<asp:EntityDataSource ID="peopleQuery" runat="server" 
    ConnectionString="name=myEntities" DefaultContainerName="myEntities"
    EnableFlattening="False" EntitySetName="people" Include="location" 
    Where="it.active = 1" OrderBy="it.lastname, it.firstname">    
</asp:EntityDataSource>

现在在我的GridView中,我可以绑定到象地址和类似城市的姓,名,EMAILADDRESS领域,并从定位表中的字段。

Now in my GridView, I can bind to fields like lastname, firstname, emailaddress, and fields from the location table like address and city.

现在,在我的系统中,用户记录可以连接到一个或一个以上的人的记录。所以这是一个1对多的关系。在GridView,我想显示与每个人相关的用户。我可以用RowDataBound事件做到这一点,但是这需要对数据库中的每一行额外的数据库查询。

Now, in my system, a user record can be attached to one or more person records. So it's a 1-to-many relationship. In the gridview, I would like to display the users associated with each person. I can do it with a RowDataBound event, but that requires an extra DB lookup for every row in the database.

是否有从1对多外键关系多结束了吗?

Is there some efficient way to navigate from the "many" end of a "1-to-many" foreign key relationship?

实际上,它甚至不必须是1对多。这可能是1比1,但你的外键关系的另一端。所以,在这个例子中,EntityDataSource使用的人的表,但在用户表中,我有一个PERSONID字段。因此,外键user.personid => PERSON.PERSONID。由于GridView控件是从用户的人表驱动,我怎么可以导航到用户表在我的GridView控件来显示user.username?

Actually, it doesn't even have to be 1-to-many. It could be 1-to-1, but you're on the other end of the foreign key relationship. So, in this example, the EntityDataSource uses the "person" table, but in the user table, I have a personid field. So the foreign key is user.personid => person.personid. Since the GridView is driven from the user person table, how can I navigate to the user table to display user.username in my GridView?

推荐答案

您可以你的GridView内,并使用通过关系数据源设置的关系,填充数据或者窝的GridView或中继器:

You can nest either a gridview or repeater inside your Gridview and use the relationship to populate the data by setting the datasource to the relationship:

<asp:EntityDataSource ID="eds" runat="server" 
     ContextTypeName="people" 
     EnableFlattening="False" 
     EntitySetName="people" 
     Include="users" >      
</asp:EntityDataSource>     

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="PeopleId" DataSourceID="eds"> 
    <Columns>             
        <asp:BoundField DataField="FirstName" HeaderText="First name"  /> 
        <asp:BoundField DataField="LastName" HeaderText="Last name" /> 
        <asp:TemplateField HeaderText="People">
            <ItemTemplate>
            <asp:Repeater ID="rptUsers" runat="server" DataSource='<%# Eval("users") %>'>
                    <ItemTemplate>
                        <%# Eval("username") %>
                    </ItemTemplate>
            </asp:Repeater>
             </ItemTemplate>
         </asp:TemplateField>
    </Columns>
</asp:GridView>

这假设你在你的数据模型有关系的设置,例如:

This assumes you have a relationship setup in your data model, eg:

public class person(){
    public int PersonId{get;set;}
    public string FirstName{ get;set;}
    public string LastName{ get;set;}
    pubic virtual ICollection<user> users { get; set; }
}

public class user(){
    public int UserId{ get;set;}
    public string username{ get;set; }
}

这篇关于EntityDataSource从GridView的另一个实体访问一个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆