I'm trying to migrate but I get this error:
Collection was modified after the enumerator was instantiated
I don't know why this happens to me. I think problem is not from foreach because I'm following a tutorial and the OnModelCreating Part is the same.
this is my context:
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);
}
}
I also have an override for SaveChanges() in my context but I think it does not relate to problem
I solved it! I should've just changed the following:
builder.Entity<User>().Property<DateTime>("InsertTime");
builder.Entity<User>().Property<DateTime?>("UpdateTime");
builder.Entity<User>().Property<DateTime?>("RemoveTime");
builder.Entity<User>().Property<bool>("IsRemoved");
To following:
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");