๐ Migration Documentation: 20230719191116_AddUserApprovedToTicketTable.cs
๐ Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- Code Breakdown
- Data Model Impact
- UML Diagram
- Usage & Examples
- Error Handling & Rollbacks
- ThirdโParty Integrations
- Conclusion
๐ 1. Overview & Purpose
This Entity Framework Core migration file adds a new boolean column UserApproved to the irby.Ticket table and provides a way to remove it if needed.
- Why?
To track whether the endโuser has approved a particular ticket in the system. - Who uses it?
Any code (services, repositories, API endpoints, UI layers) that needs to check or set theUserApprovedflag on tickets.
๐ ๏ธ 2. Dependencies & Technologies
| Technology / Package | Purpose |
|---|---|
Microsoft.EntityFrameworkCore |
Core ORM for .NET |
Microsoft.EntityFrameworkCore.Migrations |
Migration infrastructure |
Note: The project must reference EF Core and have a configured
DbContextto apply this migration.
๐ 3. Key Functional Areas
- Schema Modification
- Adds
UserApproved(typebit) with defaultfalse.
- Adds
- Migration Lifecycle
- Up: Apply the change
- Down: Revert the change
๐ 4. Code Breakdown
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IrbyImaging.DbContext.Migrations
{
/// <inheritdoc />
public partial class AddUserApprovedToTicketTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<bool>(
name: "UserApproved",
schema: "irby",
table: "Ticket",
type: "bit",
nullable: false,
defaultValue: false);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "UserApproved",
schema: "irby",
table: "Ticket");
}
}
}
4.1 Up Method
- Signature:
protected override void Up(MigrationBuilder migrationBuilder) - Function:
- Calls
AddColumn<bool> - Parameters:
name: "UserApproved"schema: "irby"table: "Ticket"type: "bit"nullable: falsedefaultValue: false
- Calls
- Outcome:
- New column created in the database with default
0(false).
- New column created in the database with default
4.2 Down Method
- Signature:
protected override void Down(MigrationBuilder migrationBuilder) - Function:
- Drops the
UserApprovedcolumn fromirby.Ticket.
- Drops the
- Outcome:
- Schema is restored to its previous state (column removed).
๐๏ธ 5. Data Model Impact
After applying this migration, the Ticket entity in your code should be updated as follows:
public class Ticket
{
public int Id { get; set; }
// ... other existing props ...
/// <summary>
/// Indicates whether the user has approved this ticket.
/// Default: false
/// </summary>
public bool UserApproved { get; set; }
}
| Column Name | CLR Type | SQL Type | Nullable | Default Value |
|---|---|---|---|---|
| UserApproved | bool | bit | No | 0 (false) |
๐ผ๏ธ 6. UML Diagram
+-------------------------------------------------+
| ยซMigrationยป AddUserApprovedToTicketTable |
|-------------------------------------------------|
| + Up(mb: MigrationBuilder): void |
| + Down(mb: MigrationBuilder): void |
+-------------------------------------------------+
+extends+
|
v
+----------------------+
| Microsoft.Entity... |
| Migration |
+----------------------+
------------------------------
| Table: Ticket (schema: irby)|
|-----------------------------|
| Id : int |
| ... |
| UserApproved : bit |
------------------------------
๐ 7. Usage & Examples
-
Generating & Applying the Migration
# 1. Add Migration dotnet ef migrations add AddUserApprovedToTicketTable \ --schema irby \ --project IrbyImaging.DbContext # 2. Apply to Database dotnet ef database update -
Querying & Updating in Code
using var context = new IrbyDbContext(); // Fetch a ticket var ticket = context.Tickets.Find(123); // Mark as approved by user ticket.UserApproved = true; context.SaveChanges(); -
LINQ Filter
// Get only user-approved tickets var approvedTickets = context.Tickets .Where(t => t.UserApproved) .ToList();
โ ๏ธ 8. Error Handling & Rollbacks
- Potential Issues:
- Column already exists โ migration will fail.
- The
Downmethod must mirrorUp. If you customizeUp, adjustDown.
- Rollback:
This executes thedotnet ef database update LastGoodMigrationNameDownmethod for this migration.
๐ 9. ThirdโParty Integrations
This migration file has no external or thirdโparty integrations beyond EF Core itself.
โ 10. Conclusion
This migration is a straightforward schema update to add UserApproved to your Ticket table. By following the steps above, you can:
- Apply the new column to your database
- Update your C# entity model
- Query and manipulate the new flag in your application
Feel free to extend this documentation with data-seeding, default-value overrides, or custom SQL if your business logic requires more advanced workflows! ๐