Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update Database Seeding Implementation for EF Core 9 Compatibility #1294

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Kamyab7
Copy link

@Kamyab7 Kamyab7 commented Jan 14, 2025

This pull request refactors the database initialization and seeding logic to utilize the new UseAsyncSeeding method introduced in EF Core 9, improving the seeding process for the Clean Architecture project. The changes ensure better asynchronous handling during database setup and enhance maintainability.

Changes:

  1. New AddAsyncSeeding Method:

    • Introduced an extension method AddAsyncSeeding that configures the DbContextOptionsBuilder to use the new UseAsyncSeeding method.
    • This method ensures that the seeding logic runs asynchronously, with service provider access to perform necessary operations like creating default roles and users.
  2. Refactored ApplicationDbContextInitialiser:

    • The seeding logic in ApplicationDbContextInitialiser was retained but refactored to work seamlessly with the new EF Core 9 seeding mechanism.
    • This includes default role and user creation, as well as seeding default data like TodoList and TodoItems.
  3. Seeding on Database Provider Configuration:

    • Updated the AddDbContext method in the AddInfrastructureServices class to integrate AddAsyncSeeding with all supported database providers (PostgreSQL, SQLite, SQL Server).
    • Ensured that the seeding logic will run after the database is created or migrated.
  4. Separate Initialization and Seeding:

    • Kept the database initialization (InitialiseAsync) and seeding (SeedAsync) separated to maintain clarity and avoid redundant operations.

Benefits:

  • Aligns the project with EF Core 9’s new async seeding approach, offering better performance and scalability.
  • Enhances code maintainability by organizing seeding into a separate method and using modern patterns.
  • Improves the overall setup process of the database and ensures default data is added seamlessly.

How to Test:

  1. Ensure that the application starts correctly with any of the supported database providers (PostgreSQL, SQLite, SQL Server).
  2. Check the database to confirm that the default roles, users, and data (like TodoList and TodoItems) are seeded correctly.
  3. Verify that the application can handle database migrations properly when starting up.

Additional Notes:

  • This change requires EF Core 9 and may require some project setup adjustments if you're working with a different version of EF Core.
  • The new seeding mechanism is backward compatible but leverages EF Core 9 features for improved efficiency.

@Kamyab7
Copy link
Author

Kamyab7 commented Jan 14, 2025

This pull request closes #1293

@Kamyab7 Kamyab7 force-pushed the feat/async-seeding-ef-core-9 branch from 1ce1ad0 to c0d0ab2 Compare January 14, 2025 15:16
…ncSeeding` method

- Introduced `AddAsyncSeeding` extension method to integrate the new `UseAsyncSeeding` approach introduced in EF Core 9.
- Refactored `ApplicationDbContextInitialiser` seeding logic to align with the new asynchronous seeding mechanism.
- Updated `AddDbContext` configuration in `AddInfrastructureServices` to use the `AddAsyncSeeding` method for all database providers (`PostgreSQL`, `SQLite`, `SQL Server`).
- Separated database initialization (`InitialiseAsync`) and seeding (`SeedAsync`) into distinct operations for better alignment with the new seeding approach.
- Maintained compatibility with dependency injection for interceptors and service provider usage.

This change modernizes the database seeding process to leverage the improved EF Core 9 capabilities, ensuring better integration with async workflows.
@Kamyab7 Kamyab7 force-pushed the feat/async-seeding-ef-core-9 branch from c0d0ab2 to fa6853d Compare January 14, 2025 15:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant