【问题标题】:Collection was modified after the enumerator was instantiated in migration枚举器在迁移中实例化后修改了集合
【发布时间】:2022-01-21 08:50:36
【问题描述】:

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

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() 的覆盖,但我认为它与问题无关

【问题讨论】:

    标签: c# asp.net-core entity-framework-core database-migration


    【解决方案1】:

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

     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");
    

    【讨论】:

      猜你喜欢
      • 2012-02-06
      • 1970-01-01
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 2020-02-17
      • 2012-04-30
      • 1970-01-01
      • 2022-09-27
      相关资源
      最近更新 更多