Session 14 Part 2
Session 14 Part 2
*Scenario:*
A healthcare management system needs to manage patients, doctors, and
appointments. It should track which doctor is treating which patient and the
appointments scheduled.
*Example:*
- *Patient Model:*
csharp
public class Patient
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Appointment> Appointments { get; set; } // Navigation
Property
}
- *Doctor Model:*
csharp
public class Doctor
{
public int Id { get; set; }
public string Name { get; set; }
public ICollection<Appointment> Appointments { get; set; } // Navigation
Property
}
- *Appointment Model:*
csharp
public class Appointment
{
public int Id { get; set; }
fi
public DateTime Date { get; set; }
public int PatientId { get; set; } // Foreign Key
public Patient Patient { get; set; } // Navigation Property
public int DoctorId { get; set; } // Foreign Key
public Doctor Doctor { get; set; } // Navigation Property
}
- *Migration Example:*
Adding new elds to the Appointment table or linking appointments with
patients and doctors.
2. *Add Migrations:*
Open the terminal or Package Manager Console and run:
bash
dotnet ef migrations add InitialCreate
3. *Update Database:*
Apply the migrations to update the database schema:
bash
dotnet ef database update
In each example, EF Code First allows you to focus on the domain model (the
business logic) while EF handles the underlying database schema. Migrations
keep the database schema in sync with your evolving model, and relationships
de ne how entities are associated, ensuring that data integrity and navigation are
maintained.
fi
fi
fi
Managing Database Schema Changes with Migrations
*Entity Framework (EF) Code First* allows you to de ne your database schema
using C# classes. When your application evolves, the database schema often
needs to change to accommodate new requirements. *Migrations* are a powerful
feature in EF Code First that help manage these schema changes in a controlled
and consistent manner.
2. *Add Migrations:*
When you change your models (e.g., add a new property, create a new model,
or update relationships), you create a migration. This migration captures the
changes you’ve made to the models.
3. *Apply Migrations:*
Migrations are applied to the database to update its schema according to the
changes described in the migration les.
4. *Migration Files:*
Each migration consists of two main parts:
- *Up Method:* Contains code to apply the changes (e.g., creating a table or
adding a column).
- *Down Method:* Contains code to revert the changes (e.g., dropping a table
or removing a column).
1. *E-Commerce Application:*
*Initial Setup:*
- *Models:* Product, Category, Manufacturer
- *Schema:* Tables for Products, Categories, and Manufacturers
*Change Scenario:*
- *Requirement:* Add a new column Discount to the Products table to store
product discounts.
*Steps:*
1. *Update Model:*
csharp
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
public int CategoryId { get; set; }
public Category Category { get; set; }
public int ManufacturerId { get; set; }
public Manufacturer Manufacturer { get; set; }
public decimal Discount { get; set; } // New Column
}
2. *Add Migration:*
bash
dotnet ef migrations add AddDiscountToProduct
3. *Apply Migration:*
bash
dotnet ef database update
*Result:* The Products table is updated with a new Discount column, and
existing data remains intact.
2. *Blog Platform:*
*Initial Setup:*
- *Models:* Post, Author
- *Schema:* Tables for Posts and Authors
*Change Scenario:*
- *Requirement:* Add a new feature to track comments on each post. You need
to create a new Comments table and establish a relationship between Posts and
Comments.
*Steps:*
1. *Create Models:*
csharp
public class Comment
{
public int Id { get; set; }
public string Content { get; set; }
public int PostId { get; set; }
public Post Post { get; set; } // Navigation Property
}
3. *Add Migration:*
bash
dotnet ef migrations add AddCommentsFeature
4. *Apply Migration:*
bash
dotnet ef database update
*Result:* A new Comments table is created, and the Posts table is updated with
the necessary relationships. Existing data is preserved.