locked
Why navigation property is required in child class RRS feed

  • Question

  • User828542847 posted

    see the code

    public class Student
    {
        public Student() { }
    
        public int StudentId { get; set; }
        public string StudentName { get; set; }
    
        public virtual StudentAddress Address { get; set; }
    
    }
         
    public class StudentAddress 
    {
        [ForeignKey("Student")]
        public int StudentAddressId { get; set; }
            
        public string Address1 { get; set; }
        public string Address2 { get; set; }
        public string City { get; set; }
        public int Zipcode { get; set; }
        public string State { get; set; }
        public string Country { get; set; }
    
        public virtual Student Student { get; set; }
    }

    so this property is in StudentAddress child class  public virtual Student Student { get; set; }

    tell me why it is required? because from parent class may have navigation property because we access child reference from parent class but ever we try to access parent property from child class?

    so discuss it here with a example why  navigation property is required in child class or show me a example where we access parent class property from child class.

    StudentAddress 
    Friday, July 21, 2017 12:44 PM

Answers

  • User-707554951 posted

    Hi tapan2017

    1. The two class (Student and StudentAddress) haven’t the relationship as parent class and child class.

    2. Navigation property is used to configure relationship between entity, e.g: one to one, one to many and many to many.

    3. If the both class have no navigation property, there are any relationship between them.

    Read the following description about navigation property:

    Navigation properties provide a way to navigate an association between two entity types. 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 a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). You may also choose to have one-way navigation, in which case you define the navigation property on only one of the types that participates in the relationship and not on both.

    If you still have doubt about this, you could read the following article carefully for full understanding

    https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 28, 2017 7:15 AM

All replies

  • User-707554951 posted

    Hi tapan2017

    In the Entity Framework, navigation properties provide a way to navigate a relationship between two entity types. 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 a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). Code First infers relationships based on the navigation properties defined on your types.

    In addition to navigation properties, we recommend that you include foreign key properties on the types that represent dependent objects. Any property with the same data type as the principal primary key property and with a name that follows one of the following formats represents a foreign key for the relationship: '<navigation property name><principal primary key property name>', '<principal class name><primary key property name>', or '<principal primary key property name>'. If multiple matches are found then precedence is given in the order listed above. Foreign key detection is not case sensitive. When a foreign key property is detected, Code First infers the multiplicity of the relationship based on the nullability of the foreign key. If the property is nullable then the relationship is registered as optional; otherwise the relationship is registered as required.

    If a foreign key on the dependent entity is not nullable, then Code First sets cascade delete on the relationship. If a foreign key on the dependent entity is nullable, Code First does not set cascade delete on the relationship, and when the principal is deleted the foreign key will be set to null. The multiplicity and cascade delete behavior detected by convention can be overridden by using the fluent API.

    https://msdn.microsoft.com/en-nz/data/jj679962.aspx

    https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx

    Best regards

    Cathy

    Monday, July 24, 2017 3:05 AM
  • User828542847 posted

    @Cathy please describe it with a example code that for what advantage we will use navigation property in child class.

    in what kind of scenario people get parent reference from child object ?

    Monday, July 24, 2017 11:22 AM
  • User-707554951 posted

    Hi tapan2017

    1. The two class (Student and StudentAddress) haven’t the relationship as parent class and child class.

    2. Navigation property is used to configure relationship between entity, e.g: one to one, one to many and many to many.

    3. If the both class have no navigation property, there are any relationship between them.

    Read the following description about navigation property:

    Navigation properties provide a way to navigate an association between two entity types. 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 a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). You may also choose to have one-way navigation, in which case you define the navigation property on only one of the types that participates in the relationship and not on both.

    If you still have doubt about this, you could read the following article carefully for full understanding

    https://msdn.microsoft.com/en-us/library/jj713564(v=vs.113).aspx

    Best regards

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 28, 2017 7:15 AM
  • User828542847 posted
    Due to navigation property fk is created?
    Suppose when we work with existing table in db then can't we omit nav property in child class in case 1 to 1 or 1 to many relation?

    Clarify these points.
    Friday, July 28, 2017 8:17 PM
  • User-707554951 posted

    Hi  tapan2017

    You don’t need to use nav property in child class to configure 1.

    However, you need to define one to one or many relationship in database

    One  to One:

    CREATE TABLE [dbo].[Course]
    
    (
    
           [CourseID] INT NOT NULL PRIMARY KEY, 
    
        [CourseName] NVARCHAR(50) NOT NULL
    
    )
    
    CREATE TABLE [dbo].[OnlineCourse]
    
    (
    
           [CourseID] INT NOT NULL PRIMARY KEY, 
    
        [CourseName] NVARCHAR(50) NULL,
    
           CONSTRAINT [FK_Course] FOREIGN KEY ([CourseID]) REFERENCES [dbo].[Course] ([CourseID]) ON DELETE CASCADE
    
    )

    Course and OnlineCourse have one to one  or zero relationship.

    One to Many:

    1. Define two tables (example A and B), with their own primary key
    2. Define a column in Table A as having a Foreign key relationship based on the primary key of Table B

    http://www.tech-recipes.com/rx/56738/one-to-one-one-to-many-table-relationships-in-sql-server/

    Best regards

    Cathy

    Monday, July 31, 2017 1:50 AM