What do Entity Framework Navigation Properties do?

.net entity-framework

Question

I can see several of these navigation attributes in my EF diagram, but I'm not clear what their actual purposes are. As you can see, I have aspnet Users attributes in several of my tables.

Why are they needed? Do they offer joins assistance? what else?

Error 2
Error 3007: Problem in Mapping Fragments starting at lines 1201, 1423: 
Non-Primary-Key column(s) [Field2] are being mapped in both fragments 
to different conceptual side properties - data inconsistency is possible 
because the corresponding conceptual side properties can be independently 
modified.
1
28
4/20/2011 4:54:21 PM

Accepted Answer

You may travel from one "linked" object to another by using a navigation attribute.

For instance, if your user is linked to a role, you may read and check the role linked to the user using the "Role" menu.

EDIT:

In order for EF to automatically load those navigation properties for you, you must explicitly include the "Role" entity in your LINQ query if you wish to load the user using LINQ-to-Entities and also look at its "Role" navigation property.

  // load user no. 4 from database
   User myUser = from u in Users.Include("Role")
                 where u.ID = 4
                 select u;

   // look at the role the user has
   string roleName = myUser.Role.Name;

OR:

   // load user no. 4 from database
   User myUser = from u in Users
                 where u.ID = 4
                 select u;

   // check to see if RoleReference is loaded, and if not, load it
   if(!myUser.RoleReference.IsLoaded)
   {
      myUser.RoleReference.Load();
      // now, the myUser.Role navigation property should be loaded and available
   }

   // look at the role the user has
   string roleName = myUser.Role.Name;

The link between two objects is essentially the programmatic equivalent of a foreign key relationship in a database. In essence, it resolves or "hides" a join between two tables (or two entities, in EF speak).

Marc

46
4/28/2019 1:58:21 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow