Seed Data Asp.Net core Identity .Net 9

Saeed Ghafori 0 Reputation points
2024-12-21T06:56:08.37+00:00

Hello, good time.

A problem in the new version of .Net 9 that I recently found after upgrading from version 8 to 9.

Regarding the seed data context identity

I found this problem after upgrading the version, in your project I have a Context, one for the project, one for mediating users with Identity. Now after upgrading the version I had a set of seed data for the IdentityUser and UserRole tables, which when I used the migration commands to Update Database, the seed data was not entered into the database at all and the database was not created. The problem was resolved by going back from .Net 9 to version 8. The error was as follows:

Update-Database -Context IdentityContext Build started... Build succeeded. System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'IdentityContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'. at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`1.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, TParam arg) at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType) at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String connectionString, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String connectionString, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'IdentityContext' has pending changes. Add a new migration before updating the database. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.

.NET
.NET
Microsoft Technologies based on the .NET software framework.
4,012 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,706 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ping Ni-MSFT 4,805 Reputation points Microsoft Vendor
    2024-12-27T07:44:34.5166667+00:00

    Hi Saeed Ghafori,

    From your error message. Please run Add-Migration to ensure the database schema matches the current model before running Update-Database.

    Add-Migration IdentitySeedFix -Context IdentityContext
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Rena

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.