'System.Stackoverflowexception' When running Migration with Dbmigrator

Asked

Viewed 116 times

4

I have my Migration that 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’ve currently tested and verified is:

  1. Inside all records there is no model with the property Repeated code
  2. I tried using db.Savechanges() between an X number in X records (from 1000 to 1000)

[Edit] The call of this Migrator is using Setinitializer

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

Where the class CustomAndMigrationDatabase receives Dbcontext and Configuration, thus performing the Dbmigrator instance as follows:

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

    Ask the context constructor as well. Possibly the problem is there.

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 all requests), you ask the Entity Framework to check the base to see if it is up to date, which executes it here:

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

Only, when performing a Migration, which are two steps (update and Seed), in the second stage (Seed), you have it somewhere:

var db = new DbContext();

Who calls again the builder, who calls again the Migration, and so on.

If I were you, I’d leave this Migrator for the time being.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.