'System.StackOverflowException' When executing migration with DbMigrator

4

I have my Migration which has a number close to 6500 records like this:

  db.MinhaLista.AddOrUpdate(x => x.Codigo, (new MeuModel { Codigo = "ABC1234", Nome = "Teste "}));

However while running my migrations as follows:

 var migration = new DbMigrator(new Configuration());
 migration.Update();

Causes the following exception:

An unhandled exception of type 'System.StackOverflowException' occurred
//Descrição:
{Cannot evaluate expression because the current thread is in a stack overflow state.}

What I currently tested and checked was:

  • Within all records there is no model with property Repeated code
  • I tried to use db.SaveChanges () between an X number in X records (1000 to 1000)
  • [Edit] The call to this migrator is using SetInitializer

     static DbContext()
        {
          Database.SetInitializer<AppDbContext>(new CustomAndMigrationDatabase<DbContext, Configuration>());
        }
    

    Where class CustomAndMigrationDatabase receives DbContext and Configuration, thus performing the instance of DbMigrator as follows:

     var migration = new DbMigrator(new TMigrationsConfiguration());
     if (migration.GetPendingMigrations().Any())
     {
       migration.Update();
     }
    
        
    asked by anonymous 26.08.2015 / 20:24

    1 answer

    1

    The problem is this initializer:

    static DbContext()
    {
        Database.SetInitializer<AppDbContext>(new CustomAndMigrationDatabase<DbContext, Configuration>());
    }
    

    When creating a new context (that is, in any and every request), you ask the Entity Framework to check the base to see if it is updated, which executes it here:

    var migration = new DbMigrator(new TMigrationsConfiguration());
    if (migration.GetPendingMigrations().Any())
    {
        migration.Update();
    }
    

    However, when running a Migration check, there are two steps (update and Seed ) in the second step Seed , you have it somewhere:

    var db = new DbContext();
    

    That calls back the builder, which again calls Migration , and so on.

    In its place, it would drop this Migrator for now.

        
    26.08.2015 / 21:15