📑 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Key Functional Areas
  4. Data Models Introduced
  5. Migration Methods: Up() & Down()
  6. UML Diagram
  7. Usage & Examples
  8. EF Core CLI Commands
  9. Third‑Party Integrations
  10. Conclusion

1. Overview & Purpose

This file, 20250115194445_AddCityAndCountyTables.cs, is an Entity Framework Core migration that adds two new tables—City and County—to the database schema.

  • 🏙️ City table (under the irby schema)
  • 🌐 County table (default schema)

Migration: A version-controlled way of evolving your database schema in lock-step with your EF Core model.


2. Dependencies & Technologies

Component Version / Type
.NET 6.0+ / 7.0+
Entity Framework Core (EF Core) 6.0+ / 7.0+
C# 10.0+
Database SQL Server / Azure SQL etc.
  • Microsoft.EntityFrameworkCore.Migrations
  • System (for Guid)

3. Key Functional Areas

  1. Creating Tables
    • City (PK: CityId)
    • County (PK: CountyId)
  2. Specifying Schemas
    • irby schema used for City table
    • Default schema for County table
  3. Rolling Back
    • Drops tables if migration is reverted

4. Data Models Introduced

City

Column Type Nullable Description
CityId uniqueidentifier No Primary Key (GUID)
Name nvarchar(max) Yes Name of the city
State nvarchar(max) Yes State or region of the city

County

Column Type Nullable Description
CountyId uniqueidentifier No Primary Key (GUID)
Name nvarchar(max) Yes Name of the county
City nvarchar(max) Yes (Optional) City to which it belongs
State nvarchar(max) Yes State or region of the county

Note: The City property on the County table is a string, not a foreign key. If you need referential integrity, consider altering the model to use CityId GUID.


5. Migration Methods: Up() & Down()

public partial class AddCityAndCountyTables : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        // 1️⃣ Create City table under 'irby' schema
        migrationBuilder.CreateTable(
            name: "City",
            schema: "irby",
            columns: table => new
            {
                CityId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                Name   = table.Column<string>(type: "nvarchar(max)", nullable: true),
                State  = table.Column<string>(type: "nvarchar(max)", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_City", x => x.CityId);
            });

        // 2️⃣ Create County table under default schema
        migrationBuilder.CreateTable(
            name: "County",
            columns: table => new
            {
                CountyId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
                Name     = table.Column<string>(type: "nvarchar(max)", nullable: true),
                City     = table.Column<string>(type: "nvarchar(max)", nullable: true),
                State    = table.Column<string>(type: "nvarchar(max)", nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_County", x => x.CountyId);
            });
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        // 🔄 Rollback: Drop tables in reverse order
        migrationBuilder.DropTable(name: "City", schema: "irby");
        migrationBuilder.DropTable(name: "County");
    }
}
  • Up(): Implements schema changes
  • Down(): Reverts them

6. UML Diagram

classDiagram
    class City {
      +Guid CityId
      +string Name
      +string State
    }
    class County {
      +Guid CountyId
      +string Name
      +string City
      +string State
    }
    %% (Note: No direct FK relationship in this migration)

7. Usage & Examples

1. Add Migration

dotnet ef migrations add AddCityAndCountyTables \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

2. Apply Migration

dotnet ef database update \
    --project IrbyImaging.DbContext \
    --startup-project IrbyImaging.Api

3. Querying Tables in C#

using var context = new IrbyDbContext();

// Add a city
context.Cities.Add(new City {
    CityId = Guid.NewGuid(),
    Name   = "Springfield",
    State  = "IL"
});
context.SaveChanges();

// Retrieve counties
var countiesInIL = context.Counties
    .Where(c => c.State == "IL")
    .ToList();

8. EF Core CLI Commands

Command Description
migrations add <Name> Scaffold a new migration
migrations remove Remove the last migration (if not applied)
database update Apply pending migrations to the database
database update <MigrationName> Roll forward or back to a specific migration

9. Third‑Party Integrations

  • None in this migration.
  • Works out-of-the-box with any EF Core–compatible database provider (SQL Server, PostgreSQL, MySQL, etc.)

10. Conclusion

🎉 What’s been achieved?

  • Added two foundational tables—City and County—to support geographic data.
  • Enabled the application to store and query location-based records.

🚀 Next Steps

  • Update your DbContext to include DbSet<City> and DbSet<County>.
  • Ensure any business logic (e.g., validations, foreign keys) is reflected in your domain models.
  • Consider seeding baseline data (states, default cities/counties) if needed.

Generated by EF Core Migration: AddCityAndCountyTables