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
  • Adding a new property to an entity
  • Add new property
  1. Basics of EF Core

Adding a property

Adding a new property to an entity

As the application grows, we will want to add new properties to existing entities let's try doing this and see what happens

Add new property

Replace the student class with the one shown below

namespace EFGetStarted
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool IsEnrolled { get; set; }
        public string City { get; set; }
    }
}

Run the application now with the following code in the program class

using EFGetStarted;
using System;
using System.Linq;

var db = new MyContext();

Console.WriteLine("List of students...\n");

foreach (var s in db.Students)
    Console.WriteLine($" Id: {s.Id}\t Name: {s.Name} Is Enrolled: {s.IsEnrolled}");

Console.WriteLine();
Console.WriteLine("Press any key to exit");
Console.ReadKey();
What happens?

You will see that the application throws an exception saying that the city column is invalid!

This is because the entity or the Student model is not In Sync with the database!

Let us see how to fix this issue next.

PreviousUpdate & DeleteNextAdd migration

Last updated 2 years ago