Question

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. 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. 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.

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. I have created a parameter for the data source that holds the ISN of the current user.

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.

Was it helpful?

Solution

In your EntityDataSource control markup, add the 'Include' attribute.

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. 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. 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). The final 'Where' expression should look something like this:

      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.

It's important to note that this approach will only work if you use TemplateField columns. This wont work if you use BoundField columns with your GridView.

If this doesn't help, please supply the error messages you are getting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top