Skip to content

Implement EF Core Seed Data for Better Development Experience #89

@hootanht

Description

@hootanht

🌱 Implement EF Core Seed Data for Better Development Experience

📋 Issue Overview

We need to implement seed data functionality using Entity Framework Core to provide a better development experience for the Refhub_Ir project. This will ensure that developers have consistent test data when working on the application locally.

🎯 Objectives

  • Implement EF Core seed data for all entities in the system
  • Create realistic and diverse test data for development
  • Ensure data consistency across different development environments
  • Provide a foundation for testing and demonstration purposes

🔍 Current State Analysis

Existing Entities in the System:

  1. ApplicationUser (Identity-based)
  2. Category - Book categories
  3. Author - Book authors
  4. Book - Main book entity
  5. Keyword - Book keywords/tags
  6. BookAuthor - Many-to-many relationship
  7. BookKeyword - Many-to-many relationship
  8. BookRelation - Book relationships

Current Configuration Files:

  • ✅ Entity configurations exist in Data/Configuration/
  • ✅ DbContext properly configured with relationships
  • ❌ No seed data implementation

🛠 Technical Requirements

R&D Keywords for Developer Research:

  1. "EF Core Data Seeding" - Official Microsoft documentation
  2. "HasData Entity Framework Core" - Primary seeding method
  3. "EF Core Seed Data Best Practices" - Implementation patterns
  4. "Entity Framework ModelBuilder HasData" - Technical implementation
  5. "EF Core Identity Seeding" - For ApplicationUser seeding
  6. "EF Core Migration Seed Data" - Data persistence across migrations
  7. "Fluent API HasData" - Configuration approach
  8. "EF Core Seed Data JSON" - External data source patterns

Sample Implementation Pattern:

// Example for CategoryConfiguration.cs
public void Configure(EntityTypeBuilder<Category> builder)
{
    // Existing configuration...
    
    // Seed Data
    builder.HasData(
        new Category { Id = 1, Name = "Programming", slug = "programming", Description = "Programming and software development books" },
        new Category { Id = 2, Name = "Science", slug = "science", Description = "Scientific research and studies" },
        new Category { Id = 3, Name = "Literature", slug = "literature", Description = "Classic and modern literature" }
    );
}

📊 Mermaid Diagram - Current vs Target State

graph TB
    subgraph "Current State"
        A1[AppDbContext] --> B1[Entity Configurations]
        B1 --> C1[Empty Database]
        C1 --> D1[Manual Data Entry Required]
        D1 --> E1[Inconsistent Dev Experience]
    end
    
    subgraph "Target State"
        A2[AppDbContext] --> B2[Entity Configurations + Seed Data]
        B2 --> C2[Pre-populated Database]
        C2 --> D2[Automatic Test Data]
        D2 --> E2[Consistent Dev Experience]
        
        B2 --> F2[Categories Seeded]
        B2 --> G2[Authors Seeded]
        B2 --> H2[Books Seeded]
        B2 --> I2[Keywords Seeded]
        B2 --> J2[Users Seeded]
        B2 --> K2[Relationships Seeded]
    end
    
    style A1 fill:#ffcccc
    style A2 fill:#ccffcc
    style E1 fill:#ffcccc
    style E2 fill:#ccffcc
Loading

🔄 Data Flow Diagram

flowchart TD
    A[Migration Run] --> B{Check Seed Data}
    B -->|Not Exists| C[Create Seed Data]
    B -->|Exists| D[Skip Seeding]
    
    C --> E[Seed Categories]
    E --> F[Seed Authors]
    F --> G[Seed Users]
    G --> H[Seed Keywords]
    H --> I[Seed Books]
    I --> J[Seed Relationships]
    J --> K[Database Ready]
    
    D --> K
    
    style C fill:#e1f5fe
    style K fill:#c8e6c9
Loading

📝 Implementation Plan

Phase 1: Basic Entity Seeding

  • Update CategoryConfiguration.cs with seed data
  • Update AuthorConfiguration.cs with seed data
  • Update KeywordConfiguration.cs with seed data
  • Create seed data for ApplicationUser (requires special handling for Identity)

Phase 2: Complex Entity Seeding

  • Update BookConfiguration.cs with seed data
  • Seed relationship tables (BookAuthor, BookKeyword, BookRelation)
  • Ensure referential integrity in seed data

Phase 3: Testing and Validation

  • Create migration for seed data
  • Test seed data in fresh database
  • Validate relationships and constraints
  • Document seeded data structure

📁 Files to be Modified

Configuration Files (Add seed data):

  1. Data/Configuration/CategoryConfiguration.cs
  2. Data/Configuration/AuthorConfiguration.cs
  3. Data/Configuration/KeywordConfiguration.cs
  4. Data/Configuration/BookConfiguration.cs
  5. Data/Configuration/BookAuthorConfiguration.cs
  6. Data/Configuration/BookKeywordConfiguration.cs
  7. Data/Configuration/BookRelationConfiguration.cs

Potential New Files:

  1. Data/SeedData/SeedDataExtensions.cs (if using extension method approach)
  2. Data/SeedData/IdentitySeedData.cs (for ApplicationUser seeding)

Documentation Updates:

  1. README.md - Update setup instructions
  2. Add seed data documentation

💡 Recommended Seed Data Structure

Categories (5-7 categories):

  • Programming & Technology
  • Science & Research
  • Literature & Fiction
  • Business & Management
  • Health & Medicine
  • History & Biography
  • Arts & Culture

Authors (15-20 authors):

  • Mix of real and fictional authors
  • Diverse backgrounds and specialties

Books (30-50 books):

  • Multiple books per category
  • Various page counts and file types
  • Realistic metadata

Keywords (20-30 keywords):

  • Technology-related tags
  • Subject-specific terms
  • General descriptive tags

Users (5-10 test users):

  • Admin user
  • Regular users with different roles
  • Test accounts for development

⚠️ Technical Considerations

Identity Seeding Challenges:

  • ApplicationUser requires password hashing
  • Consider using UserManager for proper Identity seeding
  • May need custom seeding approach outside of HasData

Relationship Seeding:

  • Many-to-many relationships need careful handling
  • Ensure foreign key constraints are satisfied
  • Consider seeding order dependencies

Migration Impact:

  • Seed data will be included in migrations
  • Consider data conflicts in existing databases
  • Plan for production deployment strategy

🕒 Estimated Development Time

Developer Time Breakdown:

  • Research & Planning: 4-6 hours
  • Basic Entity Seeding: 8-12 hours
  • Complex Relationships: 6-8 hours
  • Identity User Seeding: 4-6 hours
  • Testing & Validation: 4-6 hours
  • Documentation: 2-4 hours

Total Estimated Time: 28-42 hours (3.5-5.5 days)

Skill Level Required:

  • Intermediate Entity Framework Core knowledge
  • Basic understanding of ASP.NET Core Identity
  • Intermediate C# and database design concepts

📋 Acceptance Criteria

  • All entities have meaningful seed data
  • Database migrations include seed data
  • Fresh database setup includes test data automatically
  • Relationships between entities are properly seeded
  • At least one admin user and several regular users are created
  • Seed data is realistic and useful for development/testing
  • Documentation is updated with seed data information
  • No breaking changes to existing functionality

🔗 Additional Resources

Microsoft Documentation:

Community Resources:

  • EF Core seeding best practices
  • Identity seeding patterns
  • Performance considerations for large seed datasets

Priority: High
Type: Enhancement
Complexity: Medium
Impact: Development Experience Improvement

Metadata

Metadata

Labels

Type

Projects

Status

In Progress

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions