2

I have a table defined in my Entity model. I also have the foreign key Navigation properties defined on the tables in the model.

Users
- UserID
- Username
- UserGroupID

Groups
- GroupID
- GroupName

I have a grid connected to an EntityDataSource, which retrieves and displays the Users table. Instead of displaying the UserGroupID identity column for each user, I need to display the corresponding GroupName. Is there an easy built in way to grab the GroupName from the User object since they are connected in the entity model?

Thanks! Kevin

Kevin
  • 1,940
  • 3
  • 24
  • 38
  • I am binding the User table to a grid. As design time, the columns are retrieved. My goal is to not show the UserGroupID column, and instead display the GroupName column from the foreign key table. I'm just using the EntityDataSource wizard to point to the users table. How do I specify myUser.UserGroup.Name? I do have the navigational properties set. They are set automatically due to my foreign key's in the SQL database. – Kevin Jan 25 '12 at 15:48
  • I guess my problem is that I have the navigational properties all defined, but when I set up my EntityDataSource, it's only retrieving columns from the User table. Are you saying it should be retrieving the User table and the UserGroups table, due to the defined relationships? Because that isn't happening unfortunately. – Kevin Jan 25 '12 at 16:10

2 Answers2

3

In this tutorial, the GridView that displays the Instructors table does what you are trying to do with EntityDataSource and GridView (display a value from a navigation property):

http://www.asp.net/web-forms/tutorials/getting-started-with-ef/the-entity-framework-and-aspnet-getting-started-part-4

tdykstra
  • 5,880
  • 2
  • 23
  • 20
1

You should setup an association on each entity. For Code First it should look something like this:

class User {
  // user properties...

  [ForeignKey("UserGroupID")]
  public virtual Group Group { get; set; }
}

class Group {
  // group properties...
  public virtual ICollection<User> Users { get;set; }
}

Then you can just call:

user.Group.GroupName
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • How do I do this? Everything is defined in the graphical edmx file. Where is the class actually defined? – Kevin Jan 25 '12 at 15:35
  • I guess you are using the Model First approach, while I had incorrectly assumed you were using Code First. See - http://msdn.microsoft.com/en-us/library/bb738477.aspx for how you can use the graphical tools to set an association – PinnyM Jan 25 '12 at 15:38
  • OK I know how to add the association. When I bind the grid at design time, will the UserGroup foreign key table be available (as columns) when I point the EntityDataSource to the Users table? – Kevin Jan 25 '12 at 15:43
  • The foreign key 'UserGroup' will be available, but the cell won't know how to render that since it represents a 'Group' object and not the 'GroupName' string that you are expecting. You'll either need a separate getter property on User for this purpose, or you'll need to modify the column binding to get to the 'GroupName'. See http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/2e8f6214-e242-4a81-b49e-50bce1ee3bfe for ways to do this. – PinnyM Jan 25 '12 at 16:06
  • I found this blog post in your link: http://blogs.msdn.com/b/msdnts/archive/2007/01/19/how-to-bind-a-datagridview-column-to-a-second-level-property-of-a-data-source.aspx That is exactly what I need to do, but unfortunately my model is created in the model designer, not within C# code. The example given in the blog post uses standard C# classes and code. – Kevin Jan 25 '12 at 16:13
  • You can generate the C# classes from your model designer as covered here: http://stackoverflow.com/questions/2464909/generate-poco-classes-in-different-project-to-the-project-with-entity-framework – PinnyM Jan 25 '12 at 16:20