π¦ IrbyImaging.DbContext/Migrations/20240306145546_AddColumnsToEquipment.Designer.cs Documentation
π Table of Contents
- Overview & Purpose π
- File Structure & Location π
- Dependencies & Technologies π
- Key Functional Areas π
- Entities Affected βοΈ
- New Columns on
Equipmentβ - How EF Core Migrations Work ποΈ
- Code Snippets & Examples π»
- UML Diagram π
- Conclusion π―
1. Overview & Purpose π
AddColumnsToEquipment.Designer.cs is an auto-generated EF Core migration designer file. It represents the target model snapshot for the migration named 20240306145546_AddColumnsToEquipment.
- Purpose:
- Capture the final EntityβFramework model after applying the migration that adds new βremarksβ columns to the
Equipmenttable. - Enable EF Core to compare schemas and generate SQL to bring the database in line with the code model.
- Capture the final EntityβFramework model after applying the migration that adds new βremarksβ columns to the
2. File Structure & Location π
IrbyImaging.DbContext/
ββ Migrations/
ββ 20240306145546_AddColumnsToEquipment.cs β Migration logic
ββ 20240306145546_AddColumnsToEquipment.Designer.cs β Model snapshot
- Designer file: Contains
BuildTargetModel(ModelBuilder modelBuilder), which fully configures all entities, their properties, relationships, and database mappings after the migration is applied.
3. Dependencies & Technologies π
| Technology / Library | Version | Role |
|---|---|---|
| Microsoft.EntityFrameworkCore | 7.0.7 | ORM for .NET β model building & migrations |
| Microsoft.EntityFrameworkCore.SqlServer | 7.0.x | SQL Server provider; uses identity columns |
| .NET 6 / .NET 7 SDK | N/A | Target framework |
Note: This file is autoβgenerated by the
dotnet ef migrations addcommand.
4. Key Functional Areas π
-
[DbContext(typeof(IrbyContext))]
Marks that this snapshot belongs to theIrbyContext. -
[Migration("20240306145546_AddColumnsToEquipment")]
Associates this snapshot with the AddColumnsToEquipment migration applied at2024-03-06 14:55:46. -
BuildTargetModel(ModelBuilder modelBuilder)- Configures all entities (
Asset,Employee,Equipment, β¦) - Maps CLR types β database tables, columns, keys, indexes, relationships
- Applies TphMappingStrategy for certain hierarchies (e.g.,
AssetβComputer,MobileDevice, etc.)
- Configures all entities (
5. Entities Affected βοΈ
While the snapshot covers hundreds of tables and views, the focus of this migration is the Equipment entity.
Below is a condensed view of the pre-existing vs postβmigration properties.
6. New Columns on Equipment β
This migration adds two new properties to the Equipment table:
| Column Name | CLR Type | SQL Type | Nullable | Description |
|---|---|---|---|---|
| AssetRemark | string |
nvarchar(max) |
βοΈ | Additional free-form remark on equipment. |
| AssetRemark2 | string |
nvarchar(max) |
βοΈ | Secondary remark field for equipment notes. |
Before
modelBuilder.Entity<Equipment>(b => {
b.Property<Guid>("EquipmentId").ValueGeneratedOnAdd();
b.Property<string>("SerialNumber");
b.Property<string>("CurrentStatus");
// β¦ other existing props β¦
});
After
modelBuilder.Entity("IrbyImaging.DomainClasses.Equipment", b =>
{
b.Property<Guid>("EquipmentId")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
// β¦ existing properties β¦
b.Property<string>("AssetRemark")
.HasColumnType("nvarchar(max)");
b.Property<string>("AssetRemark2")
.HasColumnType("nvarchar(max)");
// β¦ remaining properties β¦
b.HasKey("EquipmentId");
b.ToTable("Equipment", "irby");
});
7. How EF Core Migrations Work ποΈ
-
Design-Time Snapshot
- EF Core keeps a model snapshot (in
.Designer.cs) of yourDbContextafter each migration.
- EF Core keeps a model snapshot (in
-
Comparisons
- When you run
dotnet ef migrations add XYZ, EF Core:- Compares your current model in code vs. the last snapshot.
- Detects differences (e.g., new properties).
- When you run
-
Generating Migration Files
- Creates two files:
20240306145546_AddColumnsToEquipment.csβ Contains Up()/Down() SQL operations.20240306145546_AddColumnsToEquipment.Designer.csβ New snapshot after changes.
- Creates two files:
-
Applying Migrations
- On
dotnet ef database update, EF runs the Up() steps and replaces the snapshot with this Designer.
- On
8. Code Snippets & Examples π»
Registering Migrations in Startup.cs
services.AddDbContext<IrbyContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("IrbyDb")));
Adding the Migration
# In Package Manager Console:
dotnet ef migrations add AddColumnsToEquipment
# This produces both .cs and .Designer.cs files
Updating Database
dotnet ef database update
9. UML Diagram π
classDiagram
class Equipment {
+Guid EquipmentId
+string AssetNumber
+string AssetManufacturer
+string SerialNumber
+string CurrentStatus
+string AssetRemark <<new>>
+string AssetRemark2 <<new>>
+DateTime? DateAcquired
+DateTime? DateDisposed
// β¦ other props β¦
}
class Job {
+Guid JobId
+string Name
// β¦
}
Equipment --> Job : JobId (FK)
- Note:
Equipment.JobIdis a required foreign key β each Equipment belongs to one Job.
10. Conclusion π―
AddColumnsToEquipment.Designer.csis a snapshot of your EF model after adding two new remark fields to theEquipmententity.- It is critical for EF Core to track schema changes, generate diff scripts, and ensure your database stays in sync with your C# domain classes.
π§ Tip: Never handβedit the
.Designer.csβalways let EF Core regenerate it via migrations.