πŸ“‘ Documentation: 20220509142849_UpdateEmployeeTable.cs


πŸ“š Table of Contents

  1. Overview & Purpose
  2. Key Functional Areas
  3. Schema Changes
  4. Up Migration
  5. Down Migration
  6. Dependencies & Technologies
  7. Class Diagram (UML)
  8. Usage & Examples
  9. 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:

  • BusinessUnit
  • DisplayName
  • Email

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.
Email 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:

  1. AddColumn<T> instructs EF Core to add a column of type T (here, string) to an existing table.
  2. schema and table parameters precisely locate the table.
  3. type maps to the database-side SQL type (nvarchar(max)).
  4. nullable: true means 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 TABLE statements 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 add
    • dotnet 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: UpdateEmployeeTable extends EF Core’s abstract Migration.
  • Responsibilities: Implement Up & Down to 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! πŸš€