admin管理员组

文章数量:1122832

I'm working on an ASP.NET Core Web API using Entity Framework Core. As I tried to update my database I get this error:

An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'ContextoDb' 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'.

Now the first thing I did was simply try to undo my last migration, which only added some seeder data to the IdentityRole table that is autogenerated via the Identity.EntityFrameworkCore now I did created migrated first the table with the context and all and then I realized I needed some seeder data in order to work as I wanted to in the controllers.

So I just added:

var roles = new List<IdentityRole>
            {
                new IdentityRole
                {
                    Name = "Admin",
                    NormalizedName = "ADMIN"
                },
                new IdentityRole
                {
                    Name = "User",
                    NormalizedName = "USER"
                }
            };

modelBuilder.Entity<IdentityRole>().HasData(roles); 

to the AppContext. Now the migrations add works correctly, the problem occurs when the database is updated.

Some things I tried was just undo all my migrations and delete the migrations folder.

When that didn't workout I tried doing it with a entirely different database. Thinking it might have something to do with trying to implement a seeder after the table has been created.

But that also didn't work out. Now I understand the error states that I could ignore this warning. Which I did by adding:

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

to the AppContext.

I am not entirely sure if this is the correct way to handle this or what implications or complication will it make me have to work around in the future.

So if anyone knows why is this error showing up. Any way to fix it in a more secure way I would be more than happy to know about it.

If it means anything - this is the complete AppContext:

public class ContextoDb : IdentityDbContext<AppUsuario>
{
    public ContextoDb(DbContextOptions<ContextoDb> options) : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Persona>()
                    .HasIndex(p => p.Carnet).IsUnique();

        modelBuilder.Entity<Persona>()
                    .HasData(
                new Persona
                {
                    Id = 1,
                    Nombre = "Ismael",
                    Apellido = "Moron",
                    Carnet = "12597382"
                }
            );

        modelBuilder.Entity<Hijo>().HasData(
                new Hijo
                {
                    Id = 1,
                    Nombre = "Poto",
                    FechaNacimiento = DateOnly.Parse("2020-10-21"),
                    PersonaId = 1
                }
            );

        var roles = new List<IdentityRole>
            {
                new IdentityRole
                {
                    Name = "Admin",
                    NormalizedName = "ADMIN"
                },
                new IdentityRole
                {
                    Name = "User",
                    NormalizedName = "USER"
                }
            };

        modelBuilder.Entity<IdentityRole>().HasData(roles);

        base.OnModelCreating(modelBuilder);
    }

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

    public DbSet<Persona> Personas { get; set; }
    public DbSet<Hijo> Hijos { get; set; }
}

The migration error only shows up when I try to add the seeder if I want to just add the tables without any seeder the migration will succeed and the database will be updated.

本文标签: cWhat are the implications of suppressing EF Core39s PendingModelChangesWarningStack Overflow