I have the following tables represented in my Entity Framework diagram (.edmx file)
Users
- UserID
- Username
- UserGroupID
Groups
- GroupID
- GroupName
In my code, I retrieve a valid instance of the User object and I’m trying to traverse the relationship to get to the Groups table, to retrieve the GroupName, however everytime, the Groups object is null. The UserGroupID exists in the Groups table, so i’m not sure why this is.
The Visual Studio intellisense knows the relationship exists and allows me to attempt it, but at runtime, the ‘Groups’ instance is null.
Users users= (Users)e.Row.DataItem;
string groupName = users.Groups.GroupName;
In that case, Groups is null and i’m not sure why. What are the possible causes?
Thanks
Kevin
Looks like you are trying to access Groups from within a databound event and the Groups data was not loaded before you binded it. You most likely need to “include” Groups like below.