Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Navigation Properties in Entity Framework for?

I see in my EF diagram alot of these navigation properties but not sure what they are really for. Like I see in lots of my tables I have aspnet_Users properties.

What are these for? Do they help for joins? or what?

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. 
like image 883
chobo2 Avatar asked Aug 11 '09 19:08

chobo2


People also ask

What is the use of navigation properties in Entity Framework?

A navigation property is an optional property on an entity type that allows for navigation from one end of an association to the other end. Unlike other properties, navigation properties do not carry data. A navigation property definition includes the following: A name.

What are scalar and navigation properties in Entity Framework?

Basically a scalar property is mapped to a column (int, string, ...) A navigation property is mapped to a relation. e.g Order. OrderDetails brings you to all ORderDetails of a specific order.

What is navigation property in LINQ?

Navigation properties allow a user to navigate from one entity to another, or from one entity to related entities through an association set. This topic provides examples in query expression syntax of how to navigate relationships through navigation properties in LINQ to Entities queries.

Why navigation properties are virtual?

If you define your navigation property virtual , Entity Framework will at runtime create a new class (dynamic proxy) derived from your class and uses it instead of your original class. This new dynamically created class contains logic to load the navigation property when accessed for the first time.


1 Answers

A navigation property allows you to navigate from one entity to a "connected" entity.

E.g. if your user is connected to a role, you can use the "Role" navigation to read and inspect the role associated with the user.

EDIT:

If you want to load the user with LINQ-to-Entities, and also look at its "Role" navigation property, you have to explicitly include the "Role" entity in your LINQ query - EF does NOT load those navigation properties automatically for you.

  // 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; 

It's basically a programmatic equivalent to a foreign key relationship in a database - a connection between two objects. It basically "hides" or resolves a join between two tables (or two entities, in EF speak).

Marc

like image 100
marc_s Avatar answered Nov 04 '22 14:11

marc_s