Entity Framework: Self-Referencing Table?

So I'm trying to set up a single table that will hold inventory, and have Parent/Child items – only one level of hierarchy.

It's for raw material inventory. If I have a 24×24 sheet, and cut half of it off, I'll have 12×24 leftover. If that's put back into stock, it's the same SKU (since having a unique SKU for every cut size would be ridiculous) but it's classified as a "Cut" or "Base".

So I want to have each Base as a "parent", and every Cut as a "child", without having to use multiple tables.

Each Base could have multiple Cuts or zero Cuts.

 [Key] public int InventoryId { get; set; } // Information from inventory direct public string SKU { get; set; } public string Description { get; set; } public decimal Width { get; set; } // Inches public decimal Height { get; set; } // Inches public int Qty { get; set; } // On hand, not counting reserved public decimal Cost { get; set; } public CostType CostType { get; set; } // Enum - Cost per CostType public CutType CutType { get; set; } // Enum - Base or Cut public DateTime? PurchaseOrderDate { get; set; } // Only most recent PO date // Heirarchy public int? ParentId { get; set; } 

I'm pretty new to EF, and I'm having a little trouble wrapping my head around it.

Would it just be a better practice to split this into two tables, or can I accomplish what I want to do reasonably using Code First EF?

Thanks so much.

by TripW via /r/csharp

Leave a Reply