【发布时间】: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