📑 Documentation for 20220427190253_HelpDesk_AddTicketNumber.Designer.cs
Assembly:
IrbyImaging.DbContext
Migration:20220427190253_HelpDesk_AddTicketNumber
Context:IrbyContext
Author: kvamshi
Created/Last Modified: 05-17-2022
📋 Table of Contents
- Overview & Purpose
- Dependencies & Technologies
- Key Functional Areas
- BuildTargetModel Method
- Entity Configurations
- Inheritance & Table‑Per‑Hierarchy (TPH)
- Relationships & Navigation Properties
- “HelpDesk_AddTicketNumber” Highlight
- Usage & Examples
- UML / ER Diagram
- Conclusion
1. Overview & Purpose
This file is an auto-generated EF Core Designer snapshot for the HelpDesk_AddTicketNumber migration. It describes how the Entity Framework Core model looks after the migration has been applied, enabling EF to compute diffs in future migrations.
- 📌 Purpose
- Capture the target database schema.
- Define entities, properties, keys, indexes, relationships, and inheritance rules.
- Ensure consistency between the C# model and the SQL Server database.
2. Dependencies & Technologies
| Component | Version | Role |
|---|---|---|
| Microsoft.EntityFrameworkCore | 5.0.12 | Core ORM framework |
| Microsoft.EntityFrameworkCore.SqlServer | 5.0.12 | SQL Server provider |
| Microsoft.EntityFrameworkCore.Design | 5.0.12 | Design-time tools (migrations scaffolding) |
| .NET 5 / C# 9 | — | Language & runtime |
3. Key Functional Areas
- Model Snapshot
BuildTargetModel(ModelBuilder modelBuilder)builds the complete model.
- Entity Mapping
- Fluent API calls:
.ToTable(),.HasKey(),.HasIndex(),.Property().
- Fluent API calls:
- TPH Inheritance
- Discriminator column for asset subtypes (Computer, Monitor, etc.).
- Migration Metadata
- Annotations:
Relational:MaxIdentifierLength,ProductVersion,SqlServer:ValueGenerationStrategy.
- Annotations:
4. BuildTargetModel Method
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("Relational:MaxIdentifierLength", 128)
.HasAnnotation("ProductVersion", "5.0.12")
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
// ... entity configurations follow ...
#pragma warning restore 612, 618
}
- What it does:
- Applies database-specific annotations.
- Calls a sequence of
modelBuilder.Entity(...)to configure every table.
5. Entity Configurations
Below is a slice of how several entities are configured. All tables live in the irby schema unless noted.
Ticket Entity (with new TicketNumber)
| Column | CLR Type | SQL Type | Constraints |
|---|---|---|---|
| Id | Guid |
uniqueidentifier |
PK, generated on add |
| CallBackNumber | string |
nvarchar(max) |
|
| CreatedBy | string |
nvarchar(max) |
|
| CreatedOn | DateTime |
datetime2 |
|
| CustomerName | string |
nvarchar(max) |
|
| Description | string |
nvarchar(max) |
|
| Status | int |
int |
|
| TicketNumber | int |
int |
NEW |
| Type | int |
int |
modelBuilder.Entity("IrbyImaging.DomainClasses.Ticket", b =>
{
b.Property<Guid>("Id").ValueGeneratedOnAdd().HasColumnType("uniqueidentifier");
// ... other properties ...
b.Property<int>("TicketNumber").HasColumnType("int"); // ← Added by this migration
b.HasKey("Id");
b.ToTable("Ticket", "irby");
});
🛠 Tip: To query tickets by number you could now write:
var ticket = await context.Tickets .FirstOrDefaultAsync(t => t.TicketNumber == 12345);
6. Inheritance & Table‑Per‑Hierarchy (TPH)
Asset is the base entity with a Discriminator:
modelBuilder.Entity("IrbyImaging.DomainClasses.Asset", b => {
b.Property<string>("Discriminator").IsRequired();
// ...
b.HasDiscriminator<string>("Discriminator")
.HasValue("Asset");
});
Sub‑classes:
| CLR Type | Discriminator Value |
|---|---|
Computer |
"Computer" |
DeskPhone |
"DeskPhone" |
Monitor |
"Monitor" |
MobileDevice |
"MobileDevice" |
| …and others |
All subtypes share the same
irby.Assettable, with subtype‑specific columns.
7. Relationships & Navigation Properties
The file configures foreign keys and navigations. Here are some examples:
| Principal Entity | FK Property | Dependent Entity | Navigation | Delete Behavior |
|---|---|---|---|---|
| FieldOffice | ManagerId |
FieldOffice | Manager |
Cascade |
| Employee | CurrentJobId |
Job | CurrentJob |
Cascade |
| Ticket | TicketId in Update |
Update | Updates |
Cascade |
| Asset | OwnerId |
Employee | Owner |
— |
modelBuilder.Entity("IrbyImaging.DomainClasses.Update", b =>
{
b.HasOne("IrbyImaging.DomainClasses.Ticket", null)
.WithMany("Updates")
.HasForeignKey("TicketId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
});
8. “HelpDesk_AddTicketNumber” Highlight
This migration specifically adds the TicketNumber column to the Ticket entity. The Designer snapshot captures that change so that EF can compare it to previous snapshots and generate appropriate ALTER TABLE statements.
✅ Benefit:
- Unique, sequential ticket identifiers for easier lookup in a HelpDesk scenario.
9. Usage & Examples
Applying the Migration
# 1. Scaffold the migration (already done):
dotnet ef migrations add HelpDesk_AddTicketNumber
# 2. Apply to the database:
dotnet ef database update HelpDesk_AddTicketNumber
Sample C# Code
// Create a new ticket
var newTicket = new Ticket {
CustomerName = "Alice Johnson",
Description = "Cannot print from OfficePrinter",
CreatedBy = "HelpDeskUser",
CreatedOn = DateTime.UtcNow,
Status = 0, // e.g. Open
TicketNumber = 1001, // ← Newly available
Type = 1 // e.g. Hardware
};
context.Tickets.Add(newTicket);
await context.SaveChangesAsync();
// Query by ticket number
var ticket = await context.Tickets
.SingleOrDefaultAsync(t => t.TicketNumber == 1001);
10. UML / ER Diagram
erDiagram
TICKET ||--o{ UPDATE : has
TICKET {
Guid Id
int TicketNumber
int Status
...
}
UPDATE {
Guid Id
Guid TicketId
Date Timestamp
...
}
ASSET ||--|{ ASSETMODEL : references
ASSETMODEL ||--o{ ASSETMODELDETAIL : has
ASSET ||--o{ ASSETHISTORY : tracks
EMPLOYEE ||--o{ ASSET : owns
⚙️ Note: The full model is quite extensive; this diagram gives a glimpse of the HelpDesk‑relevant portion.
11. Conclusion
This Designer snapshot is crucial for EF Core to:
- Maintain a stateful history of your model.
- Generate incremental database schema changes.
- Ensure your C# domain classes remain in sync with the SQL schema.
With the TicketNumber now part of the model, HelpDesk workflows can reliably assign, search, and manage tickets by their unique number. 🚀
Happy Coding! 🛠️