EF Core
Getting started with EF Core
Getting started with EF Core
  • Introduction
    • What is an ORM
    • Entity Framework Core
    • EF Core architecture
    • EF Core Approaches
  • Basics of EF Core
    • Create the project
      • Create the model & context
    • Create migration
    • CRUD using EF
      • Creating many entities
      • Update & Delete
    • Adding a property
      • Add migration
  • Entity Relationships
    • Add related entity
      • Add Vehicle
      • Modify Student
      • Modify context
      • Add migration
    • Manipulate related data
  • Entity Configuration
    • Fluent APIs
      • Frequently used APIs
    • Data Annotations
      • Frequently used annotations
Powered by GitBook
On this page
  1. Entity Relationships
  2. Add related entity

Modify Student

Modify the student class

Modify the student class as shown below

namespace EFGetStarted
{
    public class Student
    {
        public Student() => Vehicles = new HashSet<Vehicle>();
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsEnrolled { get; set; }
        public string City { get; set; }
        public ICollection<Vehicle> Vehicles { get; set; }
    }
}

note the following here

  • the vehicles property is called a (collection) navigation property

    • it is required as per the relationship in our domain which says one student can have many vehicles associated with him (one to many)

  • hash set is very similar to List datatype, but prevents duplicate entries, so it gives an additional safety. We could have used list collection as well

  • the constructor is initializing the Vehicles collection to avoid checking for null collection later in the code. It is not mandatory but more of a convenience

PreviousAdd VehicleNextModify context

Last updated 2 years ago