π 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
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Model Diagram (UML)
- Up() Method: Creating the Table
- Down() Method: Rolling Back
- Database Schema Snapshot
- Applying & Reverting the Migration
- Usage Examples & Code Snippets
- Error Handling & Rollback Strategies
- 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
- Migration Class Definition
- Inherits from
Migration
- Inherits from
- Up()
- Defines schema changes to apply
- Creates
Customertable
- Down()
- Defines schema changes to revert
- Drops
Customertable
π 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"
- name:
- Columns
Column Type Nullable Notes Iduniqueidentifierβ No PK, auto-generated by client or DB CustomerNumbernvarchar(max)βοΈ Yes Business-specific code Namenvarchar(max)βοΈ Yes Customer full name - Primary Key
- Named
"PK_Customer"on columnId
- Named
βͺ Down() Method: Rolling Back
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Customer",
schema: "irby");
}
- Drops the
Customertable. - 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.
- If
- Manual Rollback
- Always test
Down()to ensure it correctly removes created artifacts.
- Always test
π 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 π