首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Entity Framework中的导航属性是什么?

Entity Framework中的导航属性是什么?
EN

Stack Overflow用户
提问于 2009-08-11 19:03:11
回答 1查看 35.3K关注 0票数 28

我在我的EF图表中看到了很多这些导航属性,但不确定它们到底是用来做什么的。就像我在很多表中看到的那样,我有aspnet_Users属性。

这些是做什么用的?它们对joins有帮助吗?不然呢?

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-08-11 19:06:06

导航属性允许您从一个实体导航到一个“连接”的实体。

例如,如果您的用户连接到某个角色,您可以使用“角色”导航来读取和检查与该用户关联的角色。

编辑:

如果你想用LINQ- to -Entities加载用户,同时查看它的"Role“导航属性,你必须在你的LINQ查询中显式地包含"Role”实体- EF NOT自动为你加载这些导航属性。

代码语言:javascript
复制
  // 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;

或者:

代码语言:javascript
复制
   // 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;

它基本上等同于数据库中的外键关系--两个对象之间的连接。它基本上“隐藏”或解决了两个表(或EF中的两个实体)之间的连接。

Marc

票数 48
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1262307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档