π¦ Migration Designer: AddUARApproverToEmployeeTable
π Table of Contents
- Overview & Purpose
- Migration Context
- Key Model Change
- BuildTargetModel Method
- Entity Configuration Highlights
- Dependencies & Technologies
- Usage & Examples
- UML Class Diagram
- Developer Considerations & Best Practices
- Conclusion
1. π Overview & Purpose
File:
IrbyImaging.DbContext/Migrations/
20230718150337_AddUARApproverToEmployeeTable.Designer.cs
This is an auto-generated EF Core migration snapshot. It represents the state of your entity model after applying the migration named AddUARApproverToEmployeeTable.
Why does it exist?
- EF Core needs a βmodel snapshotβ to compare the current model vs. previous snapshots.
- The
.Designer.csfile stores all tables, columns, indexes, relationships, and annotations. - It is used by EF Core tools to generate SQL scripts for schema changes.
β οΈ DoΒ NOT manually edit this file!
Always modify your domain classes and rundotnet ef migrations add <Name>.
2. πΊ Migration Context
| Property | Value |
|---|---|
| Migration ID | 20230718150337_AddUARApproverToEmployeeTable |
| DbContext | IrbyContext |
| Target | Add column UARApprover to Employee table |
| Schema | irby |
| Provider | Microsoft SQL Server |
This migrationβs sole purpose is to add a new boolean property to the Employee entity, which will reflect in the Employee table as a column of type bit.
3. β¨ Key Model Change
| Entity | Property | CLR Type | SQL Column | SQL Type | Default |
|---|---|---|---|---|---|
| Employee | UARApprover | bool |
UARApprover |
bit |
0 (false) |
π Code Snippet: New Property Mapping
modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b =>
{
// ... existing properties ...
b.Property<bool>("UARApprover") // π₯ New boolean column
.HasColumnType("bit");
// ... keys, indexes, relationships ...
});
4. π BuildTargetModel Method
The core of this snapshot is the BuildTargetModel override:
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
modelBuilder
.HasAnnotation("ProductVersion", "7.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
// Use Identity columns for SQL Server:
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
// Define each entity in the model...
modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b =>
{
b.Property<Guid>("EmployeeId")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
// ... other existing properties ...
b.Property<bool>("UARApprover") // Newly added
.HasColumnType("bit");
b.HasKey("EmployeeId");
b.HasIndex("JdeNumber")
.IsUnique()
.HasFilter("[JdeNumber] IS NOT NULL");
b.ToTable("Employee", "irby");
// ... relationships ...
});
// ... definitions for dozens of other entities ...
}
Responsibilities of BuildTargetModel:
- Apply global annotations (e.g., EF Core version, SQL Server conventions)
- Configure identity columns
- Map every entity β table, columns, keys, indexes
- Set up relationships and navigation properties
5. π Entity Configuration Highlights
While this snapshot contains many entities, here are the most relevant for this migration:
Employee Entity
| Property | CLR Type | SQL Type | Notes |
|---|---|---|---|
| EmployeeId | Guid |
uniqueidentifier |
Primary Key, ValueGeneratedOnAdd |
| FirstName | string |
nvarchar(max) |
|
| LastName | string |
nvarchar(max) |
|
| JdeNumber | string |
nvarchar(450) |
Unique Index, Nullable |
| Status | string |
nvarchar(max) |
Required |
| UARApprover | bool |
bit |
New |
Table Mapping:
b.ToTable("Employee", "irby");
Relationship Sample
modelBuilder.Entity("IrbyImaging.DomainClasses.Employee", b =>
{
b.HasOne("IrbyImaging.DomainClasses.Department", "Department")
.WithMany()
.HasForeignKey("DepartmentId");
});
- Employee β Department (Many-to-One)
- FK:
DepartmentId
6. π οΈ Dependencies & Technologies
- .NET 7.0
- Entity Framework Core 7.0.7
- SQL Server provider
- Uses
DateTimeOffset,decimal,varbinary(max)
7. π Usage & Examples
1. Add the Migration
dotnet ef migrations add AddUARApproverToEmployeeTable
2. Update the Database
dotnet ef database update
3. Inspect Generated SQL
dotnet ef migrations script 20230718150337_AddUARApproverToEmployeeTable
Youβll see SQL akin to:
ALTER TABLE [irby].[Employee]
ADD [UARApprover] bit NOT NULL DEFAULT 0;
8. π UML Class Diagram
classDiagram
class Employee {
<<Entity>>
+Guid EmployeeId
+string FirstName
+string LastName
+string JdeNumber
+string Status
+bool UARApprover
}
class Department {
<<Entity>>
+Guid DepartmentId
+string Name
}
Employee --> Department : DepartmentId
9. π€ Developer Considerations & Best Practices
- Do NOT hand-edit
.Designer.cssnapshots. - Always update your domain classes, then run
Add-Migration. - Review migrations before applying in production.
- Use a staging environment to verify schema changes.
- Keep naming conventions consistent to avoid long rebasing.
10. β Conclusion
This Designer snapshot captures the complete model after adding the UARApprover flag to Employee. EF Core uses this to:
- Track schema changes
- Generate database update scripts
- Maintain a history of migrations
By following EF Core migration patterns, you ensure a consistent, versioned, and reliable database schema that matches your C# domain model.
π Happy coding and smooth deployments!