📦 20230713184636_AddorUpdateTablesforRBAC.Designer.cs Documentation

File Path

IrbyImaging.DbContext/Migrations/20230713184636_AddorUpdateTablesforRBAC.Designer.cs

📋 Table of Contents

  1. 🎯 Overview & Purpose
  2. 🔍 EF Core Migration Primer
  3. 🏗️ BuildTargetModel Method
    • 3.1. Model Annotations & Conventions
    • 3.2. Table & Entity Configurations
    • 3.3. Relationship & Navigation Configurations
  4. 🔑 RBAC Functional Area
    • 4.1. Role Table
    • 4.2. Permissions Table
    • 4.3. LinkRolePermission Table
    • 4.4. LinkUserRole Table
  5. ⚙️ Dependencies & Technologies
  6. 🛠️ How to Apply & Use This Migration
  7. 📝 Sample Code Snippets
  8. 🗺️ ER Diagram (Mermaid)
  9. ✅ Conclusion & Next Steps

1. 🎯 Overview & Purpose

This auto-generated EF Core Designer file captures the target model after applying the “AddorUpdateTablesforRBAC” migration.
It:

  • Defines schema annotations (SQL Server identity, max identifier length).
  • Declares all domain tables, columns, constraints, and indexes.
  • Configures relationships (FKs, cascade rules, navigation properties).
  • Introduces the RBAC (Role‑Based Access Control) tables:
    • Role
    • Permissions
    • LinkRolePermission
    • LinkUserRole

Note: You normally do not edit this file manually. It is used by EF at runtime to compare your current model to the snapshot.


2. 🔍 EF Core Migration Primer

  • Migrations: C# classes that apply (Up()) or revert (Down()) schema changes.
  • Designer File: Contains BuildTargetModel(ModelBuilder) that rebuilds the complete model snapshot for EF’s internal metadata.

Workflow

  1. You modify your domain classes or DbContext.
  2. Run dotnet ef migrations add <Name>.
  3. EF generates a Migration + Designer pair.
  4. Apply changes via dotnet ef database update.

3. 🏗️ BuildTargetModel Method

protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
    #pragma warning disable 612, 618
    modelBuilder
        .HasAnnotation("ProductVersion", "7.0.7")
        .HasAnnotation("Relational:MaxIdentifierLength", 128);

    // Configure SQL Server identity columns globally
    SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

    // ↓ Entity configurations follow ↓
    modelBuilder.Entity("IrbyImaging.DomainClasses.Role", b => { … });
    modelBuilder.Entity("IrbyImaging.DomainClasses.Permissions", b => { … });
    modelBuilder.Entity("IrbyImaging.DomainClasses.LinkRolePermission", b => { … });
    modelBuilder.Entity("IrbyImaging.DomainClasses.LinkUserRole", b => { … });
    // ... many other domain classes ...
    #pragma warning restore 612, 618
}

3.1 Model Annotations & Conventions

  • ProductVersion: Tracks EF Core version (7.0.7).
  • Relational:MaxIdentifierLength: Sets max length for SQL identifiers to 128.
  • UseIdentityColumns(): Applies SQL Server IDENTITY to integer PKs by convention.

3.2 Table & Entity Configurations

Each entity is configured via:

  • b.Property<T>(“ColumnName”) → column type, nullability, default behavior
  • b.HasKey(…) → primary key
  • b.HasIndex(…) → index (unique, filters)
  • b.ToTable(“TableName”, “irby”) → schema mapping
  • b.HasDiscriminator() → TPH inheritance for Asset subclasses

3.3 Relationship & Navigation Configurations

Relationships are expressed with:

  • b.HasOne(…)…HasForeignKey(…) → foreign key
  • .WithMany(…) → collection side
  • OnDelete(DeleteBehavior.Cascade) → cascade behavior
  • b.Navigation("PropertyName") → navigation property metadata

Example:

modelBuilder.Entity("IrbyImaging.DomainClasses.LinkUserRole", b =>
{
    b.HasOne("IrbyImaging.DomainClasses.Employee", "User")
     .WithMany()
     .HasForeignKey("EmployeeId")
     .OnDelete(DeleteBehavior.Cascade)
     .IsRequired();

    b.HasOne("IrbyImaging.DomainClasses.Role", "Role")
     .WithMany()
     .HasForeignKey("RoleId")
     .OnDelete(DeleteBehavior.Cascade)
     .IsRequired();
});

4. 🔑 RBAC Functional Area

This migration’s primary goal is to add or update the RBAC tables in the irby schema:

4.1 Role (irby.Role)

Column Type Notes
RoleId uniqueidentifier PK, generated on add
Name nvarchar(max)
Description nvarchar(max)
AddedBy nvarchar(max) Audit
AddedDate datetime2 Audit
ModifiedBy nvarchar(max) Audit
ModifiedDate datetime2 Audit
IsDeleted bit Soft‑delete flag

4.2 Permissions (irby.Permissions)

Column Type Notes
PermissionId uniqueidentifier PK, generated on add
PermissionName nvarchar(max)
PermissionDescription nvarchar(max)
AddedBy nvarchar(max) Audit
AddedDate datetime2 Audit
ModifiedBy nvarchar(max) Audit
ModifiedDate datetime2 Audit
IsDeleted bit Soft‑delete flag

4.3 LinkRolePermission (irby.LinkRolePermission)

Column Type Notes
Id uniqueidentifier PK
RoleId (FK) uniqueidentifier → Role.RoleId (Cascade delete)
PermissionId (FK) uniqueidentifier → Permissions.PermissionId (Cascade delete)
AddedBy nvarchar(max) Audit
AddedDate datetime2 Audit
ModifiedBy nvarchar(max) Audit
ModifiedDate datetime2 Audit
IsDeleted bit Soft‑delete flag

4.4 LinkUserRole (irby.LinkUserRole)

Column Type Notes
Id uniqueidentifier PK
EmployeeId(FK) uniqueidentifier → Employee.EmployeeId (Cascade delete)
RoleId (FK) uniqueidentifier → Role.RoleId (Cascade delete)
AddedBy nvarchar(max) Audit
AddedDate datetime2 Audit
ModifiedBy nvarchar(max) Audit
ModifiedDate datetime2 Audit
IsDeleted bit Soft‑delete flag

5. ⚙️ Dependencies & Technologies

  • .NET 7.0
  • Entity Framework Core 7.0 (SqlServer)
  • C# 10
  • SQL Server (I‑Series, Azure SQL, etc.)
  • Code‑First Migrations

6. 🛠️ How to Apply & Use This Migration

  1. Ensure your domain classes reflect RBAC entities (Role, Permissions, LinkRolePermission, LinkUserRole).
  2. Add migration (already generated here):
    dotnet ef migrations add AddorUpdateTablesforRBAC
    
  3. Update database:
    dotnet ef database update
    
  4. Verify in SQL Server Management Studio under schema irby—you should see the four new tables and their relationships.

7. 📝 Sample Code Snippets

7.1 Create a New Role with Permissions

using var context = new IrbyContext();

// Create new permission
var perm = new Permissions {
    PermissionName = "CanEditAsset",
    PermissionDescription = "Edit any asset",
    AddedBy = "admin", AddedDate = DateTime.UtcNow
};

// Create a role and link permission
var role = new Role {
    Name = "AssetManager",
    Description = "Manage all assets",
    AddedBy = "admin", AddedDate = DateTime.UtcNow
};

role.LinkRolePermission.Add(new LinkRolePermission {
    Role = role,
    Permissions = perm,
    AddedBy = "admin",
    AddedDate = DateTime.UtcNow
});

context.Roles.Add(role);
context.SaveChanges();

7.2 Assign Role to a User (Employee)

var user = context.Employees.Find(employeeId);
var role = context.Roles.Single(r => r.Name == "AssetManager");

var userRole = new LinkUserRole {
    User = user,
    Role = role,
    AddedBy = "system",
    AddedDate = DateTime.UtcNow
};

context.LinkUserRoles.Add(userRole);
context.SaveChanges();

8. 🗺️ ER Diagram (Mermaid)

erDiagram
    Employee ||--o{ LinkUserRole : "assigned to"
    Role     ||--o{ LinkUserRole : "assigned roles"
    Role     ||--o{ LinkRolePermission : "grants"
    Permissions ||--o{ LinkRolePermission : "granted by"

9. ✅ Conclusion & Next Steps

  • The Designer file documents the final model snapshot — do not modify manually.
  • Use the Migration file to apply or revert changes.
  • Implement your business logic around these RBAC entities:
    • Enforce role assignments in services/controllers.
    • Apply authorization policies tied to Permissions.
  • Write unit/integration tests to cover RBAC flows (e.g., “AssetManager” can EditAsset).

🎉 Happy coding and secure your app with RBAC!