views:

25

answers:

1

I'm trying to learn Entity Framework and I'm confused about navigational properties. I have a table named tblUser. It has a column named ManagerId which is has a self referencing foreign key to the UserId column. I've added this table to my EF model and I now have three "links" in my table entity properties:

tblUser1 (type: EntityCollection<tblUser>)
tblUser2 (type: tblUser)
tblUser2Reference (type: EntityReference<tblUser>)

I'm confused about for what these are used for. I want to get the manager's name for a user, which one of these should I use and how?

A: 

EF numbers your navigation properties by default, so it might be confusing. You can rename the properties in the designer though:
tblUser1 to Users;
tblUser2 to Manager;
tblUser2Reference to ManagerReference.

Now you can get the manager name like this:

user.Manager.Name;

Regarding the other properties - tblUser1 (which we renamed to Users) is the other side of your Manager navigation property. It will contain a collection of users who have the current manager. It hasn't been pluralized by default, which makes it even more confusing. If you select "Pluralize or singularize generated object names" when generating the DB, it would rather generate smth like Users1, User1 and User1Reference.

And regarding EntityReference: http://msdn.microsoft.com/en-us/library/bb297956.aspx

Represents a related end of an association with a multiplicity of zero or one.

An EntityReference object is returned by a navigation property when the related end has a multiplicity of zero or one.

Yakimych
Thanks for the info. I've found the same information checking Association property of the each navigation property and looking for what database foreign key it used. I didn't see any pluralize option when generating objects, maybe because I'm using .net 3.5. I' still don't get for what EntityReference is used for but I've found what I'm looking for I'm happy for now :)
Armagan