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

2023-09-02 01:35:37 作者:‖﹑流年因殇残缺

我在EF图很多,这些导航属性看,但不知道他们是真正的。就像我在很多我的表看我有aspnet_Users属性。

这些是什么呢?难道他们帮助的加入?还是什么?

 错误2
错误3007:在映射碎片问题起始于行1201,1423:
非主键列(图)场2]被映射到两个片段
不同的概念端属性 - 数据不一致是可能的
因为相应的侧视概念属性可以独立地
改性。
 

解决方案

一个导航属性​​,您可以浏览(废话!)从一个实体到一个连接的实体。

例如。如果您的用户连接到一个角色,您可以使用角色导航阅读并检查与该用户相关联的角色。

编辑:

C 问题 项目的目标框架不包含实体框架运行时程序集,请查看项目属性页中的目标框架信息

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

  //加载用户没有。 4从数据库
   用户MYUSER =从u在​​Users.Include(角色)
                 其中,u.ID = 4
                 选择U;

   //看用户具有的作用
   字符串角色名= myUser.Role.Name;
 

  //加载用户没有。 4从数据库
   用户MYUSER =从u的用户
                 其中,u.ID = 4
                 选择U;

   //检查,看看是否加载RoleReference,如果没有,加载它
   如果(!myUser.RoleReference.IsLoaded)
   {
      myUser.RoleReference.Load();
      //现在,myUser.Role导航属性应该被加载,并可用
   }

   //看用户具有的作用
   字符串角色名= myUser.Role.Name;
 

这基本上是一个等同于数据库中的外键关系的方案 - 两个对象之间的连接。它基本上是隐藏或可解决两个表之间的连接(或者两个实体,在EF说吧)。

马克·

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.

解决方案

A navigation property allows you to navigate (duh!) 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