📖 Table of Contents
- Introduction
- Overview & Purpose
- Dependencies & Technologies
- Migration Class Structure
- BuildTargetModel Method
- Entity Configurations
- Relationship Configurations
- UML Diagram
- How to Use This Migration
- Sample EF Core Commands
- Conclusion
1. Introduction
Welcome to the 20230721142520_AddFksToRoleAndDepartmentTables.Designer.cs migration documentation. 🎉 This file is an auto-generated EF Core Designer class that captures the target model after applying the AddFksToRoleAndDepartmentTables migration against the IrbyContext.
2. Overview & Purpose
What is this file for?
- It reconstructs the entire EF Core model snapshot at this migration point.
- EF Core uses it to compare model versions and generate SQL scripts for Up/Down migrations.
- In particular, this migration adds foreign keys linking
Role.DepartmentId → Department.DepartmentIdEmployee.RoleId → Role.RoleId(already present but re-indexed)Employee.DepartmentId → Department.DepartmentId(already present but re-indexed)
Why “AddFksToRoleAndDepartmentTables”?
- Ensures referential integrity between Roles and Departments.
- Enables efficient navigation properties in your domain model.
- Improves query performance via added indexes on FK columns.
3. Dependencies & Technologies
| Area | Details |
|---|---|
| .NET Runtime | .NET 7.0.7 |
| ORM | Entity Framework Core 7.0.7 |
| Database Provider | Microsoft SQL Server (UseIdentityColumns extension) |
| C# Language Version | C# 10+ (nullable disabled in this snapshot) |
| Tools | dotnet ef migrations add / dotnet ef database update |
4. Migration Class Structure
// Auto-generated EF Core Designer file
namespace IrbyImaging.DbContext.Migrations
{
[DbContext(typeof(IrbyContext))]
[Migration("20230721142520_AddFksToRoleAndDepartmentTables")]
partial class AddFksToRoleAndDepartmentTables
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
// ...Model configuration...
}
}
}
- Attributes:
[DbContext(typeof(IrbyContext))]links to theIrbyContext.[Migration("20230721142520_AddFksToRoleAndDepartmentTables")]names the migration.
- Key Method:
BuildTargetModel(ModelBuilder modelBuilder)defines the entire EF model schema.
5. BuildTargetModel Method
The BuildTargetModel method uses the ModelBuilder API to:
- Annotate the database provider details (
ProductVersion,Relational:MaxIdentifierLength). - Apply SQL Server identity column strategy via
UseIdentityColumns(). - Configure every entity:
- Properties (type, column name, nullability)
- Primary Keys (
HasKey) - Indexes (
HasIndex) - Table schemas and names (
ToTable)
- Define relationships and navigation properties (
HasOne,WithMany,HasForeignKey,Navigation).
6. Entity Configurations
Note: Over 80 entities are configured here. We highlight the Role & Department table changes.
Role & Department Changes
| Entity | Key Change |
|---|---|
| Role | Added DepartmentId (nullable GUID) |
Created Index on DepartmentId |
|
Defined Foreign Key to Department(DepartmentId) |
|
| Department | No structural changes – acts as the principal table. |
// Role table configuration
modelBuilder.Entity("IrbyImaging.DomainClasses.Role", b =>
{
b.Property<Guid>("RoleId").ValueGeneratedOnAdd();
// New FK property:
b.Property<Guid?>("DepartmentId").HasColumnType("uniqueidentifier");
// ...other Role properties...
b.HasKey("RoleId");
// ⚡ New Index on DepartmentId
b.HasIndex("DepartmentId");
// Map to "irby.Role"
b.ToTable("Role", "irby");
});
// Department table remains the same structurally
modelBuilder.Entity("IrbyImaging.DomainClasses.Department", b =>
{
b.Property<Guid>("DepartmentId").ValueGeneratedOnAdd();
// ...other Department properties...
b.HasKey("DepartmentId");
b.ToTable("Department", "irby");
});
7. Relationship Configurations
Below is the snippet where EF Core wires up the foreign key relationship:
modelBuilder.Entity("IrbyImaging.DomainClasses.Role", b =>
{
b.HasOne("IrbyImaging.DomainClasses.Department", "Department")
.WithMany() // One Department → Many Roles
.HasForeignKey("DepartmentId") // FK on Role.DepartmentId
.OnDelete(DeleteBehavior.Cascade);
b.Navigation("Department");
});
Cascade Delete: If a Department is removed, all linked Roles are also deleted.
Additionally, Employee entity indexes are reaffirmed:
modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b =>
{
b.HasIndex("DepartmentId"); // Ensures queries on Employee by Dept are fast
b.HasIndex("RoleId"); // Ensures queries on Employee by Role are fast
// Relationship wiring for Employee → Department & Role exists elsewhere
});
8. UML Diagram
classDiagram
Department <|-- Role : DepartmentId
Department <|-- Employee : DepartmentId
Role <|-- Employee : RoleId
- Department is the principal table.
- Role holds a FK to Department.
- Employee holds FKs to both Role and Department.
9. How to Use This Migration
- Create the migration (already done):
dotnet ef migrations add AddFksToRoleAndDepartmentTables - Inspect the generated Designer file (this document).
- Apply to the database:
dotnet ef database update - Verify that:
irby.Roletable has aDepartmentIdcolumn, index, and FK constraint.- Referential integrity is enforced for Role → Department.
10. Sample EF Core Commands
# List all migrations
dotnet ef migrations list
# Apply pending migrations
dotnet ef database update
# Rollback to previous migration
dotnet ef database update <Previous_Migration_Name>
11. Conclusion
🎯 Key Takeaways
- This designer file snapshots the entire EF Core model after adding FKs between Role and Department.
- It ensures referential integrity and query performance by indexing and configuring cascade deletes.
- Although auto-generated, understanding its structure helps you debug migrations and customize your domain mappings.
Happy coding! 🚀