0% found this document useful (0 votes)
23 views6 pages

Session 14 Part 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views6 pages

Session 14 Part 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

3.

*Healthcare Management System*

*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.

*How EF Code First is Used:*


- *Models:* Patient, Doctor, and Appointment classes de ne the main entities.
- *Relationships:* An Appointment links a Patient to a Doctor. Each Doctor can
have multiple Appointments, and each Patient can have multiple Appointments.
- *Migrations:* Migrations handle updates to the database schema, such as
adding new attributes or relationships.

*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.

### Running the Example

1. *Create Models and Context:*


De ne your models and DbContext as shown in the examples.

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

4. *Verify Data Management:*


Use a controller to interact with your data, ensuring that data is correctly added,
queried, and updated.

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.

#### *What Are Migrations?*

Migrations are a way to apply incremental changes to your database schema


without losing existing data. They allow you to evolve your database schema over
time as your application requirements change. Each migration represents a
speci c change to the schema.

#### *How Do Migrations Work?*

1. *Create Initial Models:*


De ne your data models using C# classes. These models represent the entities
in your database and their relationships.

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).

#### *Real-Life Examples*


fi
fi
fi
fi
Here are a few scenarios to illustrate how migrations are used:

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
}

2. *Update Post Model:*


csharp
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public ICollection<Comment> Comments { get; set; } // New 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.

#### *Why Use Migrations?*

1. *Consistency:* Migrations ensure that changes to the schema are applied


consistently across different environments (development, testing, production).

2. *Data Preservation:* Migrations apply changes without losing existing data,


making it safe to evolve your schema over time.

3. *Version Control:* Migrations can be tracked in source control (e.g., Git),


providing a history of schema changes and making it easier to understand and
manage schema evolution.

4. *Rollback Capability:* Migrations provide the ability to revert changes if


needed, allowing you to undo schema modi cations if something goes wrong.

In summary, *migrations* in EF Code First provide a structured way to handle


changes to your database schema, ensuring that updates are applied safely and
consistently while preserving your existing data.
fi

You might also like