简体   繁体   English

使用EntityDataSource从GridView的导航属性中选择所有项目

[英]Using an EntityDataSource to select all items from a navigation property for a GridView

I am new to the entity framework and have researched this with no avail.....although I may not have enough knowledge to ask the correct questions. 我是实体框架的新手,并且对此进行了无济于事的研究.....尽管我可能没有足够的知识来提出正确的问题。

I have a table called users with user_ISN as its primary key. 我有一个名为user的表,其中user_ISN作为其主键。 Users can be friends of other users, so I have a friends table that holds the user_ISN of the user and the user_ISN of a friend..creating a many to many relationship. 用户可以是其他用户的朋友,因此我有一个Friends表,该表包含用户的user_ISN和朋友的user_ISN ..创建多对多关系。 Entity framework has modeled this as a self join with the navigation property user.friends which of course brings you back to the user table. 实体框架将此模型建模为与导航属性user.friends的自联接,这当然会将您带回到user表。

I need to create a ASP.Net web page that uses a grid view to display only the friends of the currently logged in user. 我需要创建一个使用网格视图的ASP.Net网页,以仅显示当前登录用户的朋友。 I have created a parameter for the data source that holds the ISN of the current user. 我为保存当前用户的ISN的数据源创建了一个参数。

I have not been able to figure out how to select only the users who are friends of the current user. 我还无法弄清楚如何仅选择当前用户的朋友。 Everything I try gives me various errors that I don't understand. 我尝试的一切都会给我带来我不理解的各种错误。 Can you point me in the right direction? 你能为我指出正确的方向吗? I am more than willing to do the additional research, but I am at a point that I don't even know what to search for. 我非常愿意进行其他研究,但是我什至不知道要寻找什么。

Thank you. 谢谢。

In your EntityDataSource control markup, add the 'Include' attribute. 在EntityDataSource控件标记中,添加“包含”属性。

For Example: 例如:

      <asp:EntityDataSource ... Include="Friends" ... />

This works as a result of navigation properties provided by the entity framework for your tables. 这是由于实体框架为表提供的导航属性的结果。

Now, you will also need to modify the 'Where' property of your EntityDataSource control. 现在,您还需要修改EntityDataSource控件的'Where'属性。 From the properties window select the 'Where' property. 在属性窗口中,选择“位置”属性。 This will launch the 'Expression Editor' window. 这将启动“表达式编辑器”窗口。 You'll need to create a parameter for the User_ISN. 您需要为User_ISN创建一个参数。 Next, specify the 'Parameter source' from the list. 接下来,从列表中指定“参数源”。 Pick an option thats applicable to how you pass in the User_ISN to your page (for example, QueryString, if you pass the value in your url). 选择一个适用于您如何将User_ISN传递到页面的选项(例如,如果您在url中传递值,则为QueryString)。 The final 'Where' expression should look something like this: 最终的“ Where”表达式应如下所示:

      it.User_ISN == @User_ISN

At this point you've told the EntityDataSource to include all the friends of the current user in the results and you've also specified how to filter the results by User_ISN for the current user. 至此,您已经告诉EntityDataSource在结果中包括当前用户的所有朋友,并且还指定了如何通过User_ISN筛选当前用户的结果。

It's important to note that this approach will only work if you use TemplateField columns. 重要的是要注意,这种方法仅在使用TemplateField列时才有效。 This wont work if you use BoundField columns with your GridView. 如果在GridView中使用BoundField列,则此方法将无效。

If this doesn't help, please supply the error messages you are getting. 如果这样做没有帮助,请提供您收到的错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM