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.

Last updated