Skip to main content

Seeding Enums into Lookup Tables

Overview

The EnumLookupSeeder automatically seeds all enum values into lookup tables, creating a bridge between compile-time enums and runtime-extensible lookup tables.

Usage

During Database Initialization

// In your DbInitializer or migration
public static void Initialize(AppDbContext context)
{
// Seed enum values into lookup tables
EnumLookupSeeder.SeedEnumLookups(context);

// ... other seeding logic
}

During Migration

// In your migration Up() method
protected override void Up(MigrationBuilder migrationBuilder)
{
// ... create tables

// Seed enum lookups
var context = new AppDbContext(options);
EnumLookupSeeder.SeedEnumLookups(context);
}

What It Does

  1. Creates LookupTable entries for each enum value
  2. Creates EnumToLookupMapping entries to track the relationship
  3. Sets IsSystemDefined = true for all seeded values
  4. Maintains DisplayOrder based on enum order

Example Output

After seeding RodMaterial enum:

-- LookupTable entries
INSERT INTO LookupTables (Category, Name, Code, IsSystemDefined, DisplayOrder, IsActive)
VALUES
('RodMaterial', 'Graphite', 'Graphite', true, 0, true),
('RodMaterial', 'Fiberglass', 'Fiberglass', true, 1, true),
('RodMaterial', 'Composite (Graphite + Fiberglass)', 'Composite', true, 2, true),
('RodMaterial', 'Bamboo', 'Bamboo', true, 3, true),
('RodMaterial', 'Carbon Fiber', 'CarbonFiber', true, 4, true),
('RodMaterial', 'Other', 'Other', true, 5, true);

-- EnumToLookupMapping entries
INSERT INTO EnumToLookupMappings (EnumType, EnumValue, EnumNumericValue, LookupTableId)
VALUES
('RodMaterial', 'Graphite', 0, 1),
('RodMaterial', 'Fiberglass', 1, 2),
-- ... etc

Adding New Enum Seeders

To add seeding for a new enum:

// In EnumLookupSeeder.SeedEnumLookups()
SeedEnum<YourEnum>(context, "YourEnum", new Dictionary<YourEnum, string>
{
{ YourEnum.Value1, "Display Name 1" },
{ YourEnum.Value2, "Display Name 2" },
// ... etc
});

Benefits

  1. Automatic Sync: Enum values automatically available in lookup tables
  2. Consistent Display: Enum names can have user-friendly display names
  3. Extensibility: Users can add custom values alongside enum values
  4. Backward Compatible: Existing code using enums continues to work
  5. Query Flexibility: Can query all values (enum + custom) together

Querying Seeded Values

// Get all rod materials (system + custom)
var allMaterials = context.LookupTables
.Where(lt => lt.Category == "RodMaterial" && lt.IsActive)
.OrderBy(lt => lt.DisplayOrder)
.ToList();

// Get lookup for specific enum value
var graphiteLookup = EnumLookupSeeder.GetLookupForEnum(context, RodMaterial.Graphite);

// Get all lookups for enum type
var materials = EnumLookupSeeder.GetLookupsForEnumType<RodMaterial>(context);

Maintenance

  • Re-running Seeder: Safe to run multiple times - updates existing entries, creates missing ones
  • Display Names: Update display names in seeder dictionary if needed
  • New Enum Values: Add new enum values to seeder dictionary
  • Removed Enum Values: Set IsActive = false on corresponding lookup entries (don't delete)