LinkUserRole.cs 📑

Path: IrbyImaging.DomainClasses/LinkUserRole.cs
Namespace: IrbyImaging.DomainClasses


📋 Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Table & Schema Mapping 🗄️
  4. Properties & Data Annotations 🔖
  5. Entity Relationships 🔗
  6. Usage Examples 🛠️
  7. Integration with DbContext
  8. UML Class Diagram 🌐
  9. Conclusion 🎯

1. Overview & Purpose

The LinkUserRole class represents a many-to-many relationship between employees (users) and roles within the Irby Imaging domain.

  • It acts as a join table in the database (irby.LinkUserRole).
  • Provides audit fields (AddedBy, AddedDate, etc.) for tracking changes.
  • Implements soft‑delete support through the IsDeleted flag.

🎯 Primary Use‑Case:
Whenever you assign or remove roles from an employee (user), an instance of LinkUserRole is created/updated to reflect that relationship in the database.


2. Dependencies & Technologies

Technology / Package Version / Comment
.NET / C# Any modern .NET Core / .NET Framework version
Entity Framework (Code First) Uses Data Annotations for mapping
System.ComponentModel.DataAnnotations [Key], [ForeignKey], [Table]
System.ComponentModel.DataAnnotations.Schema [DatabaseGenerated]

No external third‑party integrations are required in this domain class.


3. Table & Schema Mapping 🗄️

[Table("LinkUserRole", Schema = "irby")]
public class LinkUserRole { … }
  • Schema: irby
  • Table Name: LinkUserRole

This instructs EF Code‑First to map the LinkUserRole class to the database table irby.LinkUserRole.


4. Properties & Data Annotations 🔖

Property Type Attributes Description
Id Guid [Key], [DatabaseGenerated(DatabaseGeneratedOption.None)] Primary key. Must be provided by the application (no DB-generated GUID).
EmployeeId Guid Foreign key to the Employee (user).
RoleId Guid Foreign key to the Role entity.
User Employee [ForeignKey("EmployeeId")] Navigation property to the linked Employee.
Role Role [ForeignKey("RoleId")] Navigation property to the linked Role.
IsDeleted bool Marks soft-deleted records.
AddedBy string Username or identifier of the user who created this link.
AddedDate DateTime Timestamp of when the record was created.
ModifiedBy string Username or identifier of the user who last modified this link.
ModifiedDate DateTime Timestamp of the most recent modification.

5. Entity Relationships 🔗

  • 1 LinkUserRole ↔️ 1 Employee (User)
    Mapped via EmployeeId → User navigation property.

  • 1 LinkUserRole ↔️ 1 Role
    Mapped via RoleId → Role navigation property.

In the database, this class resolves the many‑to‑many relationship between Employee and Role.


6. Usage Examples 🛠️

var link = new LinkUserRole
{
    Id           = Guid.NewGuid(),
    EmployeeId   = existingEmployee.Id,
    RoleId       = existingRole.Id,
    AddedBy      = currentUserName,
    AddedDate    = DateTime.UtcNow,
    ModifiedBy   = currentUserName,
    ModifiedDate = DateTime.UtcNow,
    IsDeleted    = false
};

dbContext.LinkUserRoles.Add(link);
dbContext.SaveChanges();

6.2. Querying Active Roles for a User

var roles = dbContext.LinkUserRoles
    .Where(lur => lur.EmployeeId == someEmployeeId && !lur.IsDeleted)
    .Select(lur => lur.Role)
    .ToList();
var link = dbContext.LinkUserRoles.Find(linkId);
if (link != null)
{
    link.IsDeleted    = true;
    link.ModifiedBy   = currentUserName;
    link.ModifiedDate = DateTime.UtcNow;
    dbContext.SaveChanges();
}

7. Integration with DbContext

public class ImagingDbContext : DbContext
{
    public DbSet<LinkUserRole> LinkUserRoles { get; set; }
    public DbSet<Employee>      Employees      { get; set; }
    public DbSet<Role>          Roles          { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // If any advanced configuration is needed, you can fluent‑API here.
        // modelBuilder.Entity<LinkUserRole>()
        //     .HasKey(lur => lur.Id)
        //     .HasOne(lur => lur.User)
        //     .WithMany(e => e.UserRoles)
        //     .HasForeignKey(lur => lur.EmployeeId);
    }
}
  • Register the DbSet<LinkUserRole> in your DbContext to enable CRUD operations.
  • Optionally, you can configure composite keys, indices, or cascade‑delete rules via the Fluent API.

8. UML Class Diagram 🌐

classDiagram
    class LinkUserRole {
        +Guid Id
        +Guid EmployeeId
        +Guid RoleId
        +bool IsDeleted
        +string AddedBy
        +DateTime AddedDate
        +string ModifiedBy
        +DateTime ModifiedDate
    }

    class Employee {
        +Guid Id
        +string Name
        … 
    }

    class Role {
        +Guid Id
        +string RoleName
        …
    }

    LinkUserRole --> Employee : EmployeeId
    LinkUserRole --> Role     : RoleId
  • Arrows indicate foreign‑key navigations.

9. Conclusion 🎯

LinkUserRole is a simple yet powerful domain class that:

  • Bridges users and roles in a many‑to‑many setup.
  • Tracks audit information and supports soft‑delete.
  • Leverages EF Code‑First annotations for schema mapping.

By integrating this class into your DbContext, you can effortlessly manage user‑role assignments, query them efficiently, and maintain a clear audit trail of changes. Enjoy leveraging this mapping in your next Irby Imaging project!