locked
I m very confused in navigation properties in entity framework how it works and how it usefull?? RRS feed

  • Question

  • User-1334042288 posted

    CAn anyone tell me detail of navigation property i m confused how its works and how its use full in making relations???thanks

    Thursday, April 2, 2015 9:47 AM

Answers

  • User281315223 posted

    Navigation properties will generally exist on an Entity to represent each of the possible relationships for the specified Entity. They provide an easy way to access related entities through code instead of performing some additional query to manually grab them.

    For instance, a Building Entity might have a navigation property for Rooms (e.g. all of the rooms that are contained within a particular Building) which would be generated by using the Foreign Key relationship between your Building and Room tables. This would allow you to use the following code to access all of your Rooms in a certain building :

    using(var context = new YourEntities())
    {
         // Get your building
         var building = context.Buildings.FirstOrDefault(b => b.BuildingId == 42);
         
         // Access all of the Rooms for your building by using the Rooms property
         var rooms = building.Rooms;
    }

    You might want to take a read through of this article on Entity Framework Relationships and Navigation Properties for a bit more information.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 9:51 AM
  • User-1821086702 posted

    I recommend you  do the following course: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    using System;
    using System.Collections.Generic;
    
    namespace ContosoUniversity.Models
    {
        public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public DateTime EnrollmentDate { get; set; }
    
    public virtual ICollection<Enrollment> Enrollments { get; set; } } }

    There's a one-to-many relationship between Student and Enrollment entities

    The Enrollments property is a navigation property. Navigation properties hold other entities that are related to this entity. In this case, the Enrollments property of a Student entity will hold all of the Enrollment entities that are related to that Student entity. In other words, if a given Student row in the database has two related Enrollment rows (rows that contain that student's primary key value in their StudentID foreign key column), that Student  entity's Enrollments navigation property will contain those two Enrollment entities. Navigation properties are typically defined as virtual so that they can take advantage of  certain Entity Framework functionality...

    If a navigation property can hold multiple entities (as in many-to-many or one-to-many relationships),  its type must be a list in which entries can be added, deleted, and  updated, such as  ICollection.

    In this example, a student can have several enrollement so the its type is ICollection, but let say that a Student could only be enrolled in one class then it would look like:

    public virtual Enrollment Enrollment{ get; set; }
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 8:19 PM
  • User1644755831 posted

    Hello husyawan,

    I believe @Rion Williams and @jaydee777 has explained in details about how the navigation properties work.

    I am just quoting from below article about navigation property.

    https://msdn.microsoft.com/en-us/library/vstudio/bb738520(v=vs.100).aspx

    "Navigation properties in the Entity Framework provide a way to navigate an association between two entity types. A navigation property is defined by the NavigationProperty Element (CSDL) in the conceptual model. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either an EntityReference, if the multiplicity is either one or zero-or-one, or an EntityCollection, if the multiplicity is many. You may also choose to have one-way navigation, in which case you can delete the navigation property"

    You might also want to look at the below articles for more understating.

    Relationships and Navigation Properties

    Entity Framework - Navigation Property Basics with Code First

    EF Code First Navigation Properties and Foreign Keys

    Hope this clarifies things for you.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 3, 2015 1:57 AM

All replies

  • User281315223 posted

    Navigation properties will generally exist on an Entity to represent each of the possible relationships for the specified Entity. They provide an easy way to access related entities through code instead of performing some additional query to manually grab them.

    For instance, a Building Entity might have a navigation property for Rooms (e.g. all of the rooms that are contained within a particular Building) which would be generated by using the Foreign Key relationship between your Building and Room tables. This would allow you to use the following code to access all of your Rooms in a certain building :

    using(var context = new YourEntities())
    {
         // Get your building
         var building = context.Buildings.FirstOrDefault(b => b.BuildingId == 42);
         
         // Access all of the Rooms for your building by using the Rooms property
         var rooms = building.Rooms;
    }

    You might want to take a read through of this article on Entity Framework Relationships and Navigation Properties for a bit more information.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 9:51 AM
  • User-1821086702 posted

    I recommend you  do the following course: http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

    using System;
    using System.Collections.Generic;
    
    namespace ContosoUniversity.Models
    {
        public class Student
        {
            public int ID { get; set; }
            public string LastName { get; set; }
            public string FirstMidName { get; set; }
            public DateTime EnrollmentDate { get; set; }
    
    public virtual ICollection<Enrollment> Enrollments { get; set; } } }

    There's a one-to-many relationship between Student and Enrollment entities

    The Enrollments property is a navigation property. Navigation properties hold other entities that are related to this entity. In this case, the Enrollments property of a Student entity will hold all of the Enrollment entities that are related to that Student entity. In other words, if a given Student row in the database has two related Enrollment rows (rows that contain that student's primary key value in their StudentID foreign key column), that Student  entity's Enrollments navigation property will contain those two Enrollment entities. Navigation properties are typically defined as virtual so that they can take advantage of  certain Entity Framework functionality...

    If a navigation property can hold multiple entities (as in many-to-many or one-to-many relationships),  its type must be a list in which entries can be added, deleted, and  updated, such as  ICollection.

    In this example, a student can have several enrollement so the its type is ICollection, but let say that a Student could only be enrolled in one class then it would look like:

    public virtual Enrollment Enrollment{ get; set; }
    

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 8:19 PM
  • User1644755831 posted

    Hello husyawan,

    I believe @Rion Williams and @jaydee777 has explained in details about how the navigation properties work.

    I am just quoting from below article about navigation property.

    https://msdn.microsoft.com/en-us/library/vstudio/bb738520(v=vs.100).aspx

    "Navigation properties in the Entity Framework provide a way to navigate an association between two entity types. A navigation property is defined by the NavigationProperty Element (CSDL) in the conceptual model. Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either an EntityReference, if the multiplicity is either one or zero-or-one, or an EntityCollection, if the multiplicity is many. You may also choose to have one-way navigation, in which case you can delete the navigation property"

    You might also want to look at the below articles for more understating.

    Relationships and Navigation Properties

    Entity Framework - Navigation Property Basics with Code First

    EF Code First Navigation Properties and Foreign Keys

    Hope this clarifies things for you.

    With Regards,

    Krunal Parekh

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, April 3, 2015 1:57 AM
  • User-1334042288 posted

    Thanks a lot all of you 

    Friday, April 3, 2015 3:42 AM