views:

1150

answers:

1

Hi

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.
+4  A: 

A navigation property allows you to navigate (duh!) from one entity to a "connected" entity.

E.g. if your users 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

marc_s
AhI am having trouble with adding some fields in. Like I have a table(lets call it tableA)Table A has 2 fields(Field 1<PK> 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
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
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
TableA has that. and aspnet_User has a property of TableA (TableA) has a 0..1 to many(aspnet_User)
chobo2
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
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
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
I get this error when I try to add Field2 to the aspnet_user tableSee original post.
chobo2
P.s I go aspnet_User(dot) and I don't see this Include()
chobo2
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
where do I check?
chobo2
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