๐Ÿ“š Documentation: 20221102175711_NewCoulmnForDocumentAttribute.cs


๐Ÿ“‘ Table of Contents

  1. Overview & Purpose
  2. Schema Changes
  3. Up Method Breakdown
  4. Down Method Breakdown
  5. Entity Diagram (ASCII/UML)
  6. Dependencies & Technologies
  7. Usage & Execution
  8. Sample Code Blocks & Examples
  9. Conclusion

๐Ÿ” Overview & Purpose

This migration file was automatically generated by Entity Framework Core to add two new columns to your database schema:

  • LinkCustomerData on the DocumentAttribute table
  • CustomerName on the Customer table

Its primary goals are:

  • ๐ŸŽฏ Extend the DocumentAttribute table with a boolean flag to indicate linkage to customer data
  • ๐ŸŽฏ Allow storing a humanโ€readable CustomerName in the Customer table

Filename
IrbyImaging.DbContext/Migrations/20221102175711_NewCoulmnForDocumentAttribute.cs

Namespace
IrbyImaging.DbContext.Migrations


๐Ÿ—๏ธ Schema Changes

Table Column Type Nullable Default Description
irby.DocumentAttribute LinkCustomerData bit No false Flag to link customer-specific data
irby.Customer CustomerName nvarchar(max) Yes NULL Full name or display name of the customer

๐Ÿ› ๏ธ Up Method Breakdown

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.AddColumn<bool>(
        name: "LinkCustomerData",
        schema: "irby",
        table: "DocumentAttribute",
        type: "bit",
        nullable: false,
        defaultValue: false);

    migrationBuilder.AddColumn<string>(
        name: "CustomerName",
        schema: "irby",
        table: "Customer",
        type: "nvarchar(max)",
        nullable: true);
}

What happens here?

  1. AddColumn for LinkCustomerData
    • Type: bool (bit in SQL Server)
    • Schema: irby
    • Table: DocumentAttribute
    • Constraints: Nonโ€nullable, default = false
  2. AddColumn for CustomerName
    • Type: string (nvarchar(max))
    • Schema: irby
    • Table: Customer
    • Constraints: Nullable

๐Ÿ”„ The Up method is applied when running dotnet ef database update, moving your schema forward.


๐Ÿงน Down Method Breakdown

protected override void Down(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropColumn(
        name: "LinkCustomerData",
        schema: "irby",
        table: "DocumentAttribute");

    migrationBuilder.DropColumn(
        name: "CustomerName",
        schema: "irby",
        table: "Customer");
}

Reverts the changes made in Up:

  • Drops the LinkCustomerData column
  • Drops the CustomerName column

๐Ÿ”„ The Down method is applied when rolling back this migration (e.g., dotnet ef database update 20221102175710_PreviousMigration).


๐Ÿ–ผ๏ธ Entity Diagram (ASCII/UML)

+---------------------+      +----------------------+
|  DocumentAttribute  |      |      Customer        |
|---------------------|      |----------------------|
| * Id (PK)           |      | * Id   (PK)         |
| * Name              |      | * Email             |
| * Value             |      | * CreatedAt         |
| + LinkCustomerData  |โ—€โ”€โ”€โ”€โ”€โ”€|                      |
+---------------------+      | + CustomerName      |
                              +----------------------+
  • Link from DocumentAttribute to Customer is logical; the boolean indicates if additional customer data is required.

โš™๏ธ Dependencies & Technologies

  • .NET 6.0+, C#
  • Entity Framework Core (>= 5.0)
  • SQL Server (via Microsoft.EntityFrameworkCore.SqlServer)
  • Migrations powered by Microsoft.EntityFrameworkCore.Tools

๐Ÿš€ Usage & Execution

  1. Add the Migration (already done):
    dotnet ef migrations add NewCoulmnForDocumentAttribute
    
  2. Apply to Database:
    dotnet ef database update
    
  3. Verify schema changes (e.g., SQL Server Management Studio):
    SELECT COLUMN_NAME, DATA_TYPE
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_SCHEMA = 'irby' 
      AND TABLE_NAME IN ('DocumentAttribute','Customer');
    

๐Ÿ’ป Sample Code Blocks & Examples

1. Querying with the New Columns

using (var ctx = new IrbyContext())
{
    // Create a new document attribute and link customer data
    var docAttr = new DocumentAttribute {
        Name = "Requires Verification",
        Value = "Yes",
        LinkCustomerData = true      // ๐Ÿ”ฅ new property
    };
    ctx.DocumentAttributes.Add(docAttr);

    // Create a new customer with CustomerName
    var cust = new Customer {
        Email = "[email protected]",
        CustomerName = "Alice Johnson" // ๐Ÿ”ฅ new property
    };
    ctx.Customers.Add(cust);

    ctx.SaveChanges();
}

2. LINQ Filter Example

// Fetch only attributes linked to customer data
var linkedAttributes = ctx.DocumentAttributes
                          .Where(a => a.LinkCustomerData)
                          .ToList();

๐Ÿ Conclusion

  • Purpose Achieved: Two new columns enrich your data modelโ€”one to flag linkage, another to store a display name.
  • Reversible: The migration is fully reversible via the Down method.
  • Next Steps:
    1. Update application code to surface LinkCustomerData and CustomerName in your UI or API.
    2. Add validation or business logic where needed (e.g., if LinkCustomerData is true, ensure CustomerName is populated).

โœจ With this migration in place, your domain model is now more expressive and capable of capturing customerโ€specific behaviors directly in the database schema.


Generated with โค๏ธ by IrbyImaging EF Core Migrations