zoukankan      html  css  js  c++  java
  • What are Navigation Properties in Entity Framework for?

    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 inUsers.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 inUsers
                     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).

  • 相关阅读:
    MySQLHA系列MHA(一)
    VS找不到约束
    Android开发被添加到桌面快捷方式
    Oracle查看和修改其最大的游标数
    Sde表结构分析
    SDE+ORACLE优化配置
    sqlplus常用命令
    ArcGIS 开发的一些知识学习点
    1.ireport基本使用
    java.net.ProtocolException: Exceeded stated content-length of: '13824' bytes
  • 原文地址:https://www.cnblogs.com/malaikuangren/p/2599602.html
Copyright © 2011-2022 走看看