在迁移中实例化枚举器后修改了集合

Collection was modified after the enumerator was instantiated in migration

我正在尝试 迁移,但出现此错误:

Collection was modified after the enumerator was instantiated

我不知道为什么会这样。我认为问题不是来自 foreach,因为我正在学习教程并且 OnModelCreating 部分是相同的。

这是我的上下文:

 public class DataBaseContext : DbContext, IDataBaseContext
{
    public DataBaseContext(DbContextOptions<DataBaseContext> options) : base(options)
    {

    }

    public DbSet<CatalogBrand> CatalogBrands { get; set; }
    public DbSet<CatalogType> CatalogTypes { get; set; }


    protected override void OnModelCreating(ModelBuilder builder)
    {
        foreach (var item in builder.Model.GetEntityTypes())
        {
            if (item.ClrType.GetCustomAttributes(typeof(AuditableAttribute), true).Length > 0)
            {
                builder.Entity<User>().Property<DateTime>("InsertTime");
                builder.Entity<User>().Property<DateTime?>("UpdateTime");
                builder.Entity<User>().Property<DateTime?>("RemoveTime");
                builder.Entity<User>().Property<bool>("IsRemoved");
            }
        }

        builder.ApplyConfiguration(new CatalogBrandEntityTypeConfiguration());
        builder.ApplyConfiguration(new CatalogTypeEntityTypeConfiguration());

        base.OnModelCreating(builder);
    }

   
}

我的上下文中也有 SaveChanges() 的覆盖,但我认为它与问题无关

我解决了! 我应该只更改以下内容:

 builder.Entity<User>().Property<DateTime>("InsertTime");
 builder.Entity<User>().Property<DateTime?>("UpdateTime");
 builder.Entity<User>().Property<DateTime?>("RemoveTime");
 builder.Entity<User>().Property<bool>("IsRemoved");

以下:

builder.Entity(item.Name).Property<DateTime>("InsertTime");
builder.Entity(item.Name).Property<DateTime?>("UpdateTime");
builder.Entity(item.Name).Property<DateTime?>("RemoveTime");
builder.Entity(item.Name).Property<bool>("IsRemoved");