实体框架中的导航属性是什么?

cho*_*bo2 27 .net entity-framework

我在EF图中看到了很多这些导航属性,但不确定它们的真正含义.就像我在很多表中看到的那样,我有aspnet_Users属性.

这些是为了什么?他们帮助加入吗?或者是什么?

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.
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 45

导航属性允许您从一个实体导航(duh!)到"连接"实体.

例如,如果您的用户已连接到角色,则可以使用"角色"导航来阅读和检查与用户关联的角色.

编辑:

如果要加载使用LINQ到实体的用户,同时也看看它的"角色"导航属性,你必须明确地包含在你的LINQ查询"角色"的实体- EF确实为你自动加载的导航性能.

  // 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;
Run Code Online (Sandbox Code Playgroud)

要么:

   // 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;
Run Code Online (Sandbox Code Playgroud)

它基本上是一个程序化的等价于数据库中的外键关系 - 两个对象之间的连接.它基本上"隐藏"或解析两个表(或两个实体,在EF说)之间的连接.