📝 Documentation: 20221103025134_DeleteColumnCustomerTable.cs Migration
Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Migration Class Structure
- Usage & Execution
- UML Class Diagram
- Conclusion
1. Overview & Purpose 🌟
This file is an Entity Framework Core migration that drops the CustomerName column from the Customer table in the irby schema.
-
File Path:
IrbyImaging.DbContext/Migrations/20221103025134_DeleteColumnCustomerTable.cs -
Intended Use:
- Modify the database schema by removing an obsolete column.
- Provide a reversible operation allowing rollback to the previous state.
Why?
Over time, application requirements evolve. When a particular property (here,CustomerName) is no longer needed, removing it keeps the schema clean and efficient.
2. Dependencies & Technologies 🛠️
| Technology | Version/Reference | Purpose |
|---|---|---|
| .NET Core / .NET 5+ | EF Core 5.0+ | ORM for database schema migrations |
| Microsoft.EntityFrameworkCore.Migrations | NuGet Package | Provides MigrationBuilder & migration scaffolding |
| SQL Server (or other RDBMS) | Provider-specific | Target database for applying schema changes |
3. Key Functional Areas 🔑
- Migration Class Definition
Up()Method – Defines the schema change to apply.Down()Method – Defines how to revert the change.
4. Migration Class Structure 🏗️
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
public partial class DeleteColumnCustomerTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CustomerName",
schema: "irby",
table: "Customer");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CustomerName",
schema: "irby",
table: "Customer",
type: "nvarchar(max)",
nullable: true);
}
}
}
4.1 Up Method 🚀
Purpose: Apply the migration.
-
Operation:
Drops theCustomerNamecolumn from theirby.Customertable. -
Key Call:
migrationBuilder.DropColumn(...) -
Parameters:
name: Column name to remove ("CustomerName").schema: Schema of target table ("irby").table: Table name ("Customer").
Code Snippet:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CustomerName",
schema: "irby",
table: "Customer");
}
4.2 Down Method 🔄
Purpose: Revert the migration.
-
Operation:
Adds back theCustomerNamecolumn with typenvarchar(max)and allows nulls. -
Key Call:
migrationBuilder.AddColumn<string>(...) -
Parameters:
name: New column name ("CustomerName").schema: Schema of target table ("irby").table: Table name ("Customer").type: SQL data type ("nvarchar(max)").nullable: Whether null values are allowed (true).
Code Snippet:
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "CustomerName",
schema: "irby",
table: "Customer",
type: "nvarchar(max)",
nullable: true);
}
5. Usage & Execution ⚙️
- Add Migration:
dotnet ef migrations add DeleteColumnCustomerTable - Review Generated File:
- Located under
Migrations/20221103025134_DeleteColumnCustomerTable.cs.
- Located under
- Apply Migration to Database:
dotnet ef database update - Rollback (if needed):
dotnet ef database update PreviousMigrationName
6. UML Class Diagram 📈
Below is a simple class diagram showing the migration structure:
+-----------------------------------------+
| Migration |
|-----------------------------------------|
| + void Up(MigrationBuilder builder) |
| + void Down(MigrationBuilder builder) |
+----------------+------------------------+
^
|
+-----------------------------------------+
| DeleteColumnCustomerTable : Migration |
|-----------------------------------------|
| <<override>> Up(builder) |
| <<override>> Down(builder) |
+-----------------------------------------+
- Inheritance:
DeleteColumnCustomerTableinherits fromMigration.
7. Conclusion ✅
In this migration file:
Upremoves the unusedCustomerNamecolumn, streamlining the schema.Downensures safe rollback by re-adding the column if necessary.
This pattern (Up/Down) is fundamental to EF Core migrations, enabling iterative, versioned schema evolution in a team-friendly and reversible manner.
🚀 Happy coding with Entity Framework Core migrations!