0
votes

There was a breaking change so we decided to reset migrations, but now If we don't put the -EnableAutomaticMigrations option Enable-Migrations doesn't work. My connection string is in my web project that is set as the startup project.

This are the steps we took:

First We deleted the Migrations folder and the database completely. Then When we do:

enable-migrations

We get:

Checking if the context targets an existing database...

Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.

I don't understand this error, there is no database, there are no migrations, to what pending changes does it refer to? (I dont even have pending chanves in git)

If then We do

enable-migrations -EnableAutomaticMigrations

It works, but when We follow it with:

Add-Migration InitialCreation

It creates an Empty InitialCreation migration and it creates the database with the tables that correspond to my model. But doing this We can't get the scripts for creating the database for the first time. I don't get what is going on. It seems the first time it uses some Automatic migration.

Why can't We enable-migrations without -EnableAutomaticMigrations?

Update

After talking with Steve Green, I think it could be useful to add part of my DbContext and Initializer

public class ApplicationDatabaseContext : DbContext
    {
        public ApplicationDatabaseContext()
            : base("MyConnectionStringName")
        {
            Database.SetInitializer<ApplicationDatabaseContext>(new ApplicationDataInitializer());

            if (!Database.Exists())
            {
                Database.Initialize(true);
            }
        }

And my DatabaseInitializer:

public class ApplicationDataInitializer : CreateDatabaseIfNotExists<ApplicationDatabaseContext>
    {
        protected override void Seed(ApplicationDatabaseContext context)
        {
            base.Seed(context);

            CreateData(context);
        }

        private void CreateData(ApplicationDatabaseContext context)
        {
            //Add data here
            context.SaveChanges();
        }
    }
1

1 Answers

0
votes

First, make sure you have the initializer MigrateDatabaseToLatestVersion set.

Second, You should not need to enable automatic migrations. To reset migrations you don't have to delete the entire folder (especially if you've done some seeding or other customizations in the Configuration() class). Just delete the individual migrations then add a new baseline migration:

add-migration MyStartPoint -IgnoreChanges

This will take a snapshot with no code in the Up() since you don't really need that to generate a script to create the database. So now update-database -Script -SourceMigration $InitialDatabase should create a script you can use to create a new database.

Another option is to use an initializer like CreateDatabaseIfNotExist which will do exactly that.