29

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.
abatishchev
  • 98,240
  • 88
  • 296
  • 433
chobo2
  • 83,322
  • 195
  • 530
  • 832

1 Answers1

48

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

George Stocker
  • 57,289
  • 29
  • 176
  • 237
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Ah I am having trouble with adding some fields in. Like I have a table(lets call it tableA) Table A has 2 fields(Field 1 and Field 2). My aspnet_userTable has all standard asp.net membership fields plus Field 1 and Field 2. when I go and try to add a new user to the aspnet_userTable I don't see Field1 or Field2. So I tried to do it in 2 steps first do the aspnet_Users.Createaspnet_Users() then store that in a aspnet_Users table(lets call it user). Then I tried user.Field1 = "something" this works. then I tried user.Field2(no property is found). I see it has this though – chobo2 Aug 11 '09 at 19:34
  • 1
    user.TableA.Field1 and user.TableA.Field2 but when I try to set it I just get some null reference error. What am I doing wrong? – chobo2 Aug 11 '09 at 19:35
  • Well, you have your associations wrong - if you add a new table "TableA" to your system, and create a foreign key relationship to "aspnet_user", then your "TableA" object will have a relation (navigation property) to "aspnet_User" - not the other way around. So in your "TableA" entity, there should be a "aspnet_User" navigation property. – marc_s Aug 11 '09 at 19:42
  • TableA has that. and aspnet_User has a property of TableA (TableA) has a 0..1 to many(aspnet_User) – chobo2 Aug 11 '09 at 20:01
  • I would assume you get the null reference exception because of the fact that EF will not automatically load the referenced entity as I explained in my edited post. You need to load those things explicitly in your code. – marc_s Aug 11 '09 at 20:19
  • That could be the reason I will check that out now. But I just don't understand if I added the columns to the aspnet table why would then not show up in the entity list. Like I add Field1 and Field2 to teh aspnet table in sql2005 express and generated my EF yet only Field1 shows up in the aspnet table as a scalar value. Field2 is nowhere to be found. – chobo2 Aug 11 '09 at 20:24
  • by the way in the above post Field2 would be the "PK" forgot that it was field one. So I don't know if that makes a difference or not. – chobo2 Aug 11 '09 at 20:25
  • I get this error when I try to add Field2 to the aspnet_user table See original post. – chobo2 Aug 11 '09 at 20:26
  • P.s I go aspnet_User(dot) and I don't see this Include() – chobo2 Aug 11 '09 at 20:33
  • If you don't see the `Include` keyword, then probably your "aspnet_User" entity isn't really a EF entity (does it derive from `EntityObject` ?? – marc_s Aug 11 '09 at 20:45
  • if you did pull the "aspnet_Users" table onto your EDMX model design surface, there's a (model).Designer.cs (or .vb) file behind this model. In there, you'll find all the Entities defined - including "aspnet_Users" and "TableA" – marc_s Aug 12 '09 at 04:43
  • Thanks, marc_s, this was exactly what I needed to know – Ethel Evans Dec 07 '10 at 18:39
  • @marc_s thank you but how is a navigation property different from a FK id? – BenKoshy Dec 14 '16 at 05:58