📚 Documentation: 20250320211658_AddIntuneTable.Designer.cs

Location: IrbyImaging.DbContext/Migrations/20250320211658_AddIntuneTable.Designer.cs
Generated By: Entity Framework Core 9.0.0
Context: Auto‑generated migration “AddIntuneTable” for the IrbyContext DbContext.


Table of Contents

  1. Overview & Purpose
  2. Dependencies & Technologies
  3. Migration Structure
  4. Key Functional Areas
  5. “Intune” Table Mapping
  6. Applying & Using the Migration
  7. Code Snippets & Examples
  8. Conclusion

1. Overview & Purpose 🚀

This Designer file is part of the EF Core Code‑First Migration named AddIntuneTable. Its job is to capture the model snapshot after the migration has been applied, so EF Core can compare schema changes over time.

  • Auto‑Generated: Do not manually modify. Changes here will be overwritten by EF Core tools.
  • BuildTargetModel: Contains a fluent API description of every entity, property, relationship and constraint in the context after the migration.

2. Dependencies & Technologies 🔧

Technology / Package Version Purpose
Microsoft.EntityFrameworkCore 9.0.0 Core ORM engine
Microsoft.EntityFrameworkCore.Migrations 9.0.0 Migration scaffolding
Microsoft.EntityFrameworkCore.SqlServer 9.0.0 SQL Server provider w/ identity columns
.NET 7+ System libraries — Runtime & data types

3. Migration Structure 🏗️

[DbContext(typeof(IrbyContext))]
[Migration("20250320211658_AddIntuneTable")]
partial class AddIntuneTable
{
    protected override void BuildTargetModel(ModelBuilder modelBuilder)
    {
        // … fluent API model definition …
    }
}
  • DbContext Attribute: Binds the migration to IrbyContext.
  • Migration Attribute: Names the migration and timestamp.
  • BuildTargetModel: Defines the final EF Core model snapshot.

4. Key Functional Areas 🎯

  1. Global Conventions

    • modelBuilder.HasAnnotation("ProductVersion", "9.0.0")
    • UseIdentityColumns() for SQL Server Identity (auto‑increment) support.
  2. Entity Definitions

    • Each modelBuilder.Entity("Namespace.DomainClasses.X", b => { ... }) block maps a CLR type to a relational table, configures keys, indexes, column types, and relationships.
  3. Relationships & Navigation

    • HasOne(...).WithMany(...) and HasForeignKey(...) calls configure one‑to‑many, many‑to‑many, and inheritance (TPH).
  4. Discriminator / TPH

    • Single‑table inheritance for Asset derivatives (Computer, MobileDevice, etc.), controlled via .HasDiscriminator().HasValue(...).
  5. New “Intune” Table

    • Added in this migration: see section below.

5. “Intune” Table Mapping 🔍

The core reason for this migration: add a new Intune entity to track Microsoft Intune device metadata.

Property Type Column Type Notes
IntuneId (PK) Guid uniqueidentifier Auto‑generated on add
AssetId (FK) Guid uniqueidentifier Links to Asset.Id
AzureADDeviceId string nvarchar(max) Azure AD device GUID
AzureADRegistered string nvarchar(max) Registration status
Category string nvarchar(max) Device category (e.g., PC)
Compliance string nvarchar(max) Compliance state
ComplianceGracePeriodExpiration DateTime datetime2 Expiration date
… (many Intune‑specific fields) … … … …
WiFiMAC string nvarchar(max) MAC address
modelBuilder.Entity("IrbyImaging.DomainClasses.Intune", b =>
{
    b.Property<Guid>("IntuneId")
     .ValueGeneratedOnAdd()
     .HasColumnType("uniqueidentifier");
    b.Property<Guid>("AssetId")
     .HasColumnType("uniqueidentifier");
    b.Property<string>("AzureADDeviceId")
     .HasColumnType("nvarchar(max)");
    // … other fields …
    b.HasKey("IntuneId");
    b.ToTable("Intune", "irby");
});

Note: No explicit foreign key is configured here—if needed, update the domain model to include navigation to Asset.


6. Applying & Using the Migration 🛠️

  1. Add Migration
    dotnet ef migrations add AddIntuneTable
    
  2. Update Database
    dotnet ef database update
    
  3. Verify
    • Check dbo.irby.Intune table in SQL Server.
    • Confirm columns and data types match the model.

7. Code Snippets & Examples 📖

Example: Domain Entity for Intune

Define your Intune domain class in IrbyImaging.DomainClasses project:

public class Intune
{
    public Guid IntuneId { get; set; }
    public Guid AssetId { get; set; }
    public string AzureADDeviceId { get; set; }
    // … other properties …

    public Asset Asset { get; set; }   // Navigation
}

Example: Querying Intune Records

using var context = new IrbyContext();
var nonCompliantDevices = context.Set<Intune>()
    .Where(i => i.Compliance == "NonCompliant")
    .Include(i => i.Asset)
    .ToList();

foreach (var device in nonCompliantDevices)
{
    Console.WriteLine($"{device.Asset.SerialNumber} ⇒ {device.Compliance}");
}

8. Conclusion 🎉

  • The Designer file is a read‑only snapshot of your model after adding the Intune table.
  • It ensures EF Core can compute diffs for future migrations.
  • To customize behavior (indexes, FKs, constraints), modify your DomainClasses or override OnModelCreating in IrbyContext, then re‑scaffold.

Happy coding and secure device management! 🔒