📚 Table of Contents
- Overview & Purpose
- Entity Framework Core Migrations
- AddCityAndCountyTables Migration
3.1. What Was Added?
3.2. BuildTargetModel Method - Data Models: City & County
4.1. City Entity
4.2. County Entity - How to Apply & Use This Migration
- Sample Code Snippets
- UML Class Diagram
- Conclusion
Overview & Purpose
🚀 AddCityAndCountyTables.Designer.cs
- This auto-generated file is part of an Entity Framework Core migration named
20250115194445_AddCityAndCountyTables. - It’s responsible for capturing the target model snapshot after adding City and County tables to the database schema.
- Do not manually edit this file—changes should be made in your migration or model classes, not in the designer.
Entity Framework Core Migrations
Entity Framework Core Migrations allow you to evolve your database schema over time.
- Migration class (e.g.
AddCityAndCountyTables.cs) containsUp()/Down()methods. - Designer file (this file) contains
BuildTargetModel(), describing the entire model snapshot at that point in time.
Why Designer Files Exist
- They provide a point-in-time snapshot so EF knows how to transition from one migration to the next.
- They are used by tooling (
dotnet ef) to compute diffs and scaffold next migrations.
AddCityAndCountyTables Migration
What Was Added?
✅ Two new tables in the irby schema:
| Table | Primary Key | Columns |
|---|---|---|
| City | CityId |
CityId, Name, State |
| County | CountyId |
CountyId, City, Name, State |
BuildTargetModel Method
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "9.0.0")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
// Use SQL Server identity columns by default
modelBuilder.UseIdentityColumns();
// ... snapshot of ALL entities ...
// New City entity
modelBuilder.Entity("IrbyImaging.DomainClasses.City", b =>
{
b.Property<Guid>("CityId")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.Property<string>("State")
.HasColumnType("nvarchar(max)");
b.HasKey("CityId");
b.ToTable("City", "irby");
});
// New County entity
modelBuilder.Entity("IrbyImaging.DomainClasses.County", b =>
{
b.Property<Guid>("CountyId")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("City")
.HasColumnType("nvarchar(max)");
b.Property<string>("Name")
.HasColumnType("nvarchar(max)");
b.Property<string>("State")
.HasColumnType("nvarchar(max)");
b.HasKey("CountyId");
b.ToTable("County");
});
// ... remainder of the model snapshot ...
}
modelBuilder.Entity(...)calls configure every entity in the DbContext—this snapshot includes every table, column, index, relationship, discriminator for TPH inheritance, and more.- Only the City and County sections are new in this migration.
Data Models: City & County
City Entity
Namespace: IrbyImaging.DomainClasses
Class: City
| Property | Type | Notes |
|---|---|---|
| CityId | Guid | Primary key, auto-generated |
| Name | string | Name of the city |
| State | string | Two‑letter state abbreviation |
C# Model (example)
public class City
{
public Guid CityId { get; set; } // PK
public string Name { get; set; } // e.g. "Austin"
public string State { get; set; } // e.g. "TX"
}
County Entity
Namespace: IrbyImaging.DomainClasses
Class: County
| Property | Type | Notes |
|---|---|---|
| CountyId | Guid | Primary key, auto-generated |
| City | string | Name of the city the county belongs to |
| Name | string | Name of the county |
| State | string | Two‑letter state abbreviation |
C# Model (example)
public class County
{
public Guid CountyId { get; set; } // PK
public string City { get; set; } // e.g. "Austin"
public string Name { get; set; } // e.g. "Travis"
public string State { get; set; } // e.g. "TX"
}
How to Apply & Use This Migration
-
Add Migration
dotnet ef migrations add AddCityAndCountyTables -
Update Database
dotnet ef database update -
Querying the New Tables
using var context = new IrbyContext(); // List all cities var cities = context.Set<City>().ToList(); // Add a new city context.Set<City>().Add(new City { Name = "Dallas", State = "TX" }); context.SaveChanges(); // Add a county context.Set<County>().Add(new County { City = "Dallas", Name = "Collin", State = "TX" }); context.SaveChanges();
Sample Code Snippets
Adding a City
var city = new City
{
Name = "Houston",
State = "TX"
};
context.Cities.Add(city);
context.SaveChanges();
Console.WriteLine($"New City ID: {city.CityId}");
Querying Counties by City
var austinCounties = context.Counties
.Where(c => c.City == "Austin")
.OrderBy(c => c.Name)
.ToList();
Updating a County
var county = context.Counties.First(c => c.Name == "Travis");
county.Name = "Travis County";
context.SaveChanges();
UML Class Diagram
+--------------------+ +----------------------+
| City | | County |
+--------------------+ +----------------------+
| - CityId : Guid | | - CountyId : Guid |
| - Name : string | | - City : string |
| - State : string | | - Name : string |
+--------------------+ | - State : string |
+----------------------+
- City and County are standalone entities—no direct EF relationship between them.
- Both inherit from
object(no TPH/TPT hierarchy here).
Conclusion
🎯 AddCityAndCountyTables.Designer.cs is a snapshot of the EF Core model at the moment City and County tables were introduced.
- Auto-generated: don’t edit by hand.
- Represents: all entities, relationships, and DB configuration.
- Newly Added:
CityandCountyunder theirbyschema.
By running your migrations, you bring your database schema in sync with this model, enabling you to CRUD City and County objects through your IrbyContext.
Happy coding! 🚀