π Documentation: 20220509142849_UpdateEmployeeTable.cs
π Table of Contents
- Overview & Purpose
- Key Functional Areas
- Schema Changes
- Up Migration
- Down Migration
- Dependencies & Technologies
- Class Diagram (UML)
- Usage & Examples
- Conclusion
π 1. Overview & Purpose
This C# file defines an Entity Framework Core migration that updates the existing Employee table in the irby schema by adding three new columns:
BusinessUnitDisplayNameEmail
The migration class is autogenerated by EF Core (dotnet ef migrations add) and allows you to evolve your database schema in a controlled, revertible manner.
File Metadata
Assembly : IrbyImaging.DbContext
Author : kvamshi
Created : 05-17-2022
Last Modified : 05-17-2022
Filename : 20220509142849_UpdateEmployeeTable.cs
Namespace : IrbyImaging.DbContext.Migrations
βοΈ 2. Key Functional Areas
| Area | Description |
|---|---|
| Migration Class | UpdateEmployeeTable inherits from Migration. |
| Up Operation | Adds three new columns to the Employee table. |
| Down Operation | Drops those columns, rolling back the schema to its previous state. |
| Schema | All changes are scoped to the irby schema. |
| Columns Added | BusinessUnit, DisplayName, Email (all nvarchar(max), nullable = true) |
ποΈ 3. Schema Changes
| Column Name | Data Type | Nullable | Purpose/Notes |
|---|---|---|---|
| BusinessUnit | nvarchar(max) | Yes | Tracks the organizational unit or department of an employee. |
| DisplayName | nvarchar(max) | Yes | Holds the user-friendly name for display within UIs/reports. |
| nvarchar(max) | Yes | Employeeβs corporate or contact email address. |
π 4. Up Migration
The Up method is called when you apply the migration to upgrade your database. It uses the MigrationBuilder API to generate SQL statements.
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "BusinessUnit",
schema: "irby",
table: "Employee",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "DisplayName",
schema: "irby",
table: "Employee",
type: "nvarchar(max)",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Email",
schema: "irby",
table: "Employee",
type: "nvarchar(max)",
nullable: true);
}
How it works:
AddColumn<T>instructs EF Core to add a column of typeT(here,string) to an existing table.schemaandtableparameters precisely locate the table.typemaps to the database-side SQL type (nvarchar(max)).nullable: truemeans existing rows wonβt break on migration (no default required).
π½ 5. Down Migration
The Down method is the inverse of Up and is used to rollback the migration if needed:
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "BusinessUnit",
schema: "irby",
table: "Employee");
migrationBuilder.DropColumn(
name: "DisplayName",
schema: "irby",
table: "Employee");
migrationBuilder.DropColumn(
name: "Email",
schema: "irby",
table: "Employee");
}
Rolling back:
- EF Core will generate
ALTER TABLEstatements to drop each of the previously added columns. - Ensures you can revert to the prior schema if something goes wrong.
π οΈ 6. Dependencies & Technologies
- Language: C#
- ORM: Entity Framework Core (Microsoft.EntityFrameworkCore)
- Database Platform: SQL Server (implied by
nvarchar(max)) - .NET SDK: .NET Core / .NET 5+
- Tooling:
dotnet ef migrations adddotnet ef database update
π 7. Class Diagram (UML)
+----------------------------------+
| Migration (abstract) |
+----------------------------------+
| + void Up(MigrationBuilder mb) |
| + void Down(MigrationBuilder mb) |
+----------------------------------+
^
|
+--------------------------------------------+
| UpdateEmployeeTable |
+--------------------------------------------+
| <<partial>> |
| + override void Up(MigrationBuilder mb) |
| + override void Down(MigrationBuilder mb) |
+--------------------------------------------+
- Inheritance:
UpdateEmployeeTableextends EF Coreβs abstractMigration. - Responsibilities: Implement
Up&Downto define schema transitions.
π 8. Usage & Examples
1. Create & Apply Migration
# Add the migration to your project
dotnet ef migrations add UpdateEmployeeTable
# Apply pending migrations (including this one)
dotnet ef database update
2. Inspect Generated SQL
You can preview the SQL before applying:
dotnet ef migrations script --from 20220509142848_PreviousMigration --to 20220509142849_UpdateEmployeeTable
3. Rollback a Migration
To revert the last applied migration:
dotnet ef database update 20220509142848_PreviousMigration
π 9. Conclusion
This migration file is a critical step in evolving the Employee table to capture more metadata (Business Unit, Display Name, Email). It follows EF Core conventions for safe, repeatable, and revertible database schema changes.
βMigrations are the version control for your database schema.β β EF Core Documentation
Keep your migrations small, atomic, and well-documented to ensure maintainability and smooth deployments across environments.
Happy Coding! π