📄 Documentation: AddStateTable Migration (20220804191712)
Module: IrbyImaging.DbContext/Migrations
File:20220804191712_AddStateTable.cs
Purpose: Introduce a State table into the database schema under theirbyschema.
📑 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Data Model: State Entity
- Migration Implementation
- 5.1 Up Method
- 5.2 Down Method
- Migration Workflow & Usage Examples
- UML Diagram
- Conclusion
1. Overview & Purpose
This Entity Framework Core migration adds a new table named State in the irby schema of the application database.
It enables the application to store standardized state data (e.g., U.S. states or other regional subdivisions) for downstream features like address validation, reporting, and localization.
Why introduce a State table?
- ✅ Normalization: Avoid repeated string literals for state names/abbreviations.
- ✅ Referential Integrity: Link addresses and entities to a consistent set of states.
- ✅ Extendibility: Easily add metadata (e.g., time zones, tax rates) in future migrations.
2. Dependencies & Technologies
| Technology / Package | Purpose |
|---|---|
| .NET 6 / .NET Core | Application framework |
EntityFrameworkCore (Microsoft.EntityFrameworkCore) |
ORM for database migrations & data access |
| EntityFrameworkCore.Migrations | Migration generation and schema evolution |
| SQL Server (or any EF Core–supported RDBMS) | Target database engine |
3. Key Functional Areas
- Schema Management: Define and version database structure using code.
- Up/Down Migration: Apply (
Up) or revert (Down) schema changes. - Table Definition: Specify columns, types, constraints, and schema.
4. Data Model: State Entity
Although the migration file itself does not include the CLR class model, you would typically pair this migration with a State entity defined in your DbContext:
using System;
namespace IrbyImaging.Models
{
public class State
{
public Guid Id { get; set; } // Primary key
public string Code { get; set; } // Numeric or custom code
public string Abbreviation { get; set; } // e.g., "CA"
public string Name { get; set; } // e.g., "California"
}
}
Then, in your DbContext:
public class IrbyContext : DbContext
{
public DbSet<State> States { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("irby");
modelBuilder.Entity<State>().ToTable("State");
base.OnModelCreating(modelBuilder);
}
}
5. Migration Implementation
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace IrbyImaging.DbContext.Migrations
{
public partial class AddStateTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "State",
schema: "irby",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
Code = table.Column<string>(type: "nvarchar(max)", nullable: true),
Abbreviation = table.Column<string>(type: "nvarchar(max)", nullable: true),
Name = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_State", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "State",
schema: "irby");
}
}
}
5.1 Up Method (Create Table)
- CreateTable: Defines a new table
irby.State. - Columns:
Id(Guid, PK)Code(nvarchar(max))Abbreviation(nvarchar(max))Name(nvarchar(max))
- Primary Key:
PK_StateonId.
5.2 Down Method (Drop Table)
- DropTable: Removes
irby.Stateschema object—reverts the schema to its previous state.
6. Migration Workflow & Usage Examples
- Add Migration
dotnet ef migrations add AddStateTable - Review Generated Code
- Check
Migrations/20220804191712_AddStateTable.cs.
- Check
- Apply to Database
dotnet ef database update - Revert Migration (if needed)
dotnet ef database update LastGoodMigration
7. UML Diagram
classDiagram
class State {
<<Entity>>
+Guid Id
+string Code
+string Abbreviation
+string Name
}
State : PK Id
Note: This high‑level class diagram shows the
Stateentity and its properties.
8. Conclusion
🎉 What we achieved
- Introduced a new
Statetable in theirbyschema. - Ensured a clear migration path (Up/Down) for schema evolution.
- Laid groundwork for any address‑ or region‑based feature in the application.
🔠Next Steps
- Seed the
Statetable with common values (e.g., 50 US states). - Build API endpoints or UI components to manage states.
- Extend the model with additional metadata (time zones, regions).
Feel free to integrate this migration into your deployment pipeline and extend it to meet your business requirements! 🚀