πŸ“‹ Documentation: 20221031021815_Customer.cs

Welcome to the detailed markdown documentation for the Entity Framework Core migration file:

IrbyImaging.DbContext/Migrations/20221031021815_Customer.cs

This document covers overview, purpose, inner workings, usage examples, UML diagrams, and more.


πŸ“‘ Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Model Diagram (UML)
  5. Up() Method: Creating the Table
  6. Down() Method: Rolling Back
  7. Database Schema Snapshot
  8. Applying & Reverting the Migration
  9. Usage Examples & Code Snippets
  10. Error Handling & Rollback Strategies
  11. Conclusion

πŸ” Overview & Purpose

This migration class is part of IrbyImaging.DbContext. Its main goal is to introduce a new database table named Customer under the irby schema.

  • Timestamp (in filename): 20221031021815
  • Migration Name: Customer

Why do we need it?

  • Track customer entities in the database.
  • Store GUID-based primary keys for distributed uniqueness.
  • Keep business identifiers (CustomerNumber) and names (Name).

πŸ› οΈ Dependencies & Technologies

Component Version / Note
.NET / .NET Core 5.x / 6.x / 7.x (any EF CoreΒ compatible)
Entity Framework Core (EF) β‰₯ 5.0
SQL Server / RDBMS Supports uniqueidentifier, nvarchar(max)
MigrationBuilder Built-in EF Core Migration API

🎯 Key Functional Areas

  1. Migration Class Definition
    • Inherits from Migration
  2. Up()
    • Defines schema changes to apply
    • Creates Customer table
  3. Down()
    • Defines schema changes to revert
    • Drops Customer table

πŸ“Š Data Model Diagram (UML)

@startuml
package irby {
  class Customer {
    + Guid Id
    + string CustomerNumber
    + string Name
  }
}
@enduml

Above UML shows the resulting Customer entity once migration is applied.


πŸš€ Up() Method: Creating the Table

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.CreateTable(
        name: "Customer",
        schema: "irby",
        columns: table => new
        {
            Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
            CustomerNumber = table.Column<string>(type: "nvarchar(max)", nullable: true),
            Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
        },
        constraints: table =>
        {
            table.PrimaryKey("PK_Customer", x => x.Id);
        });
}

Highlights

  • CreateTable(...)
    • name: "Customer"
    • schema: "irby"
  • Columns
    Column Type Nullable Notes
    Id uniqueidentifier ❌ No PK, auto-generated by client or DB
    CustomerNumber nvarchar(max) βœ”οΈ Yes Business-specific code
    Name nvarchar(max) βœ”οΈ Yes Customer full name
  • Primary Key
    • Named "PK_Customer" on column Id

βͺ Down() Method: Rolling Back

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropTable(
        name: "Customer",
        schema: "irby");
}
  • Drops the Customer table.
  • Ensures a clean rollback to the previous schema state.

πŸ—„οΈ Database Schema Snapshot

After applying this migration, the DB will contain:

Schema Table Column Data Type Null? Key
irby Customer Id uniqueidentifier No PK
irby Customer CustomerNumber nvarchar(max) Yes
irby Customer Name nvarchar(max) Yes

πŸ’‘ Applying & Reverting the Migration

Apply Migration

dotnet ef migrations add Customer                # Generate migration (if not autogenerated)
dotnet ef database update                        # Applies all pending migrations

Revert (Roll Back)

dotnet ef database update LastGoodMigrationName  # Roll back to a specific prior migration

Alternatively, to remove the last migration (if not yet applied):

dotnet ef migrations remove

πŸ”§ Usage Examples & Code Snippets

1. Adding a Customer in C#

using var context = new IrbyImagingDbContext();
var newCustomer = new Customer
{
    Id = Guid.NewGuid(),
    CustomerNumber = "CUST-2023-0001",
    Name = "Acme Corporation"
};
context.Customers.Add(newCustomer);
context.SaveChanges();

2. Querying Customers

var customer = context.Customers
                      .FirstOrDefault(c => c.CustomerNumber == "CUST-2023-0001");
Console.WriteLine(customer.Name);

⚠️ Error Handling & Rollback Strategies

  • Migration Conflicts
    • If two migrations target the same table/column, EF Core will warn. Resolve by merging or renaming.
  • Broken State
    • If Up() fails mid-way, the table will not exist and EF will throw. Use transaction support (default) to auto-rollback.
  • Manual Rollback
    • Always test Down() to ensure it correctly removes created artifacts.

πŸ“Œ Conclusion

The 20221031021815_Customer.cs migration is a straightforward yet crucial step in managing your database schema. By encapsulating creation and rollback logic for the Customer table, it provides:

  • πŸš€ Scalable and versioned DB changes
  • πŸ”„ Safe rollback capabilities
  • πŸ“Š Clear data structure for customer management

Leverage Entity Framework Core tooling to maintain consistency across development, staging, and production environments. Happy coding! πŸš€


End of Documentation πŸ˜ƒ