Why did the project show a problem with SeedData Migration during run?

2 weeks ago 15
ARTICLE AD BOX

I added the model and its SeedData. There was no problem while adding the migration or updating the database. The SeedData was also saved to the database without any problems. But during the project run, there was a problem with this error:

System.InvalidOperationException: 'An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ApplicationDbContext' has pending changes. Add a new migration before updating the database. See https://aka.ms/efcore-docs-pending-changes. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.'

When I create a new migration, Up Down is empty. There is no change and there is no point in updating.

Additional: I created a model and pushed it to GIT. Before it merged, I added another model and updated the database. After the previous code was merged, I pulled and merged them. There was no conflict.

Extension for SeedData:

catch error: await context.Database.MigrateAsync();

public static class SeedDataExtension { public static async Task ApplySeedDataAsync(this WebApplication app) { try { using var scope = app.Services.CreateScope(); var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); await context.Database.MigrateAsync(); await LocationSeedData.SeedLocationsAsync(context); } catch (Exception ex) { var logger = app.Services.GetRequiredService<ILogger<Program>>(); logger.LogError(ex, "{action} Fail: {errorMessage}", nameof(ApplySeedDataAsync), ex.Message); throw; } } }

Part used for Program.cs:

if (!isHangfireMode) { await app.ApplySeedDataAsync(); }

Solutions I've tried:

I went back one migration and removed the migration. I created a new migration and added this migration.

I deleted the ApplicationDbContextModelSnapshot.cs file, created a new migration, and updated the database.

Solutions provided by ChatGPT:

1. There is a conflict in the OnModelCreating method. The database models and the models in the schema are not compatible. (I only added a model, I didn't change the existing models.)

2. Ignore pending changes:

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); }

3. I can't kill the database.

Context:

public interface IApplicationDbContext { DbSet<CandidateInfo> CandidateInfos { get; } DbSet<VacancyInfo> VacancyInfos { get; } DbSet<WebsiteNews> WebsiteNews { get; } DbSet<WebsiteNewsLocalization> WebsiteNewsLocalizations { get; } DbSet<Location> Locations { get; } DbSet<LocationLocalizations> LocationLocalizations { get; } Task<int> SaveChangesAsync(CancellationToken cancellationToken = default); } public class ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : DbContext(options), IApplicationDbContext { public DbSet<CandidateInfo> CandidateInfos => Set<CandidateInfo>(); public DbSet<VacancyInfo> VacancyInfos => Set<VacancyInfo>(); public DbSet<WebsiteNews> WebsiteNews => Set<WebsiteNews>(); public DbSet<WebsiteNewsLocalization> WebsiteNewsLocalizations => Set<WebsiteNewsLocalization>(); public DbSet<Location> Locations => Set<Location>(); public DbSet<LocationLocalizations> LocationLocalizations => Set<LocationLocalizations>(); protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { base.OnConfiguring(optionsBuilder); optionsBuilder.ConfigureWarnings(w => w.Ignore(RelationalEventId.PendingModelChangesWarning)); } protected override void OnModelCreating(ModelBuilder builder) { builder.HasDefaultSchema("website"); builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); builder.Entity<CandidateInfo>() .HasOne(c => c.VacancyInfo) .WithMany() .HasForeignKey(c => c.VacancyId) .OnDelete(DeleteBehavior.Cascade); builder.Entity<Location>(entity => { entity.HasMany(e => e.Vacancies) .WithOne(v => v.Location) .HasForeignKey(v => v.LocationId) .OnDelete(DeleteBehavior.Cascade); entity.HasMany(e => e.Localizations) .WithOne(l => l.Location) .HasForeignKey(l => l.LocationId) .OnDelete(DeleteBehavior.Cascade); }); builder.Entity<LocationLocalizations>(entity => { entity.HasOne(e => e.Location) .WithMany(l => l.Localizations) .HasForeignKey(e => e.LocationId) .OnDelete(DeleteBehavior.Cascade); }); builder.Entity<VacancyInfo>(entity => { entity.HasOne(e => e.Location) .WithMany(l => l.Vacancies) .HasForeignKey(e => e.LocationId) .OnDelete(DeleteBehavior.Restrict); }); builder.Entity<WebsiteNewsLocalization>(entity => { entity.HasOne(e => e.WebsiteNews) .WithMany(w => w.Localizations) .HasForeignKey(e => e.WebsiteNewsId) .OnDelete(DeleteBehavior.Cascade); }); } }
Read Entire Article