0
votes

I have an ASP.NET MVC project using EF code first and migrations.

I am trying to add a new property to a class. For that I create the migration code in order to add the specific column in the table related to that class.

My migration code is the following:

public partial class Add_DataRequisicao_to_BiblRequisicao : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.BiblRequisicoes", "DataRequisicao", c => c.String(nullable: false, defaultValue: "01/01/1900 00:00"));
    }

    public override void Down()
    {
        DropColumn("dbo.BiblRequisicoes", "DataRequisicao");
    }
}

And the property to add to the class is the following:

[Required]
[Display(Name = "Data Requisição")]
public string DataRequisicao { get; set; }

When I publish the project and go to the project web site, I get the following error message:

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.

Inspecting the database, I see that the migration was run with success because the column was created. In the migration table, there also is a new entry that indicates that the migration ran.

The migration entry:

MigrationId: 201704261436437_Add_DataRequisicao_to_BiblRequisicao
ContextKey: WebAppInvestigacaoMultimedia.Migrations.Configuration   
Model: 0x1F8B0800000...
ProductVersion: 6.1.3-40302

I don't understand why I get the error message if the migration ran with success and the name and the type of the property is the same as the add column of the table.

1
How did you create that migration? Did you use Add-Migration or did you add the class yourself?Gimly
Have you using automatic or code-based migrations? From 2 methods presented in code sample I think you're using Add-Migrations, which means code-based migration. Use -Verbose -Script flag in Package Manager Console to see DB scripts and steps to perform migration process.Tetsuya Yamamoto
@Gimly I use Add-Migrationmiguelbgouveia
@TetsuyaYamamoto I want to use code-based migrations because I am more confident with this method. You said to use -Verbose -Script flags. But what is the command that I should use with these flags?miguelbgouveia

1 Answers

1
votes

This error is usually linked to changes to the model that haven't been added to a migration script yet.

If you look at the folder where you have your migrations, you should have resx files, those files contain a "snapshot" of the model at the time the migration was generated. It therefore knows if there are changes that have been made to the model and will not let you do an Update-Database if there are pending changes.

To resolve the issue, you either have to rollback any changes you made to the model since the last migration you made, or create a new migration using Add-Migration and then do Update-Database.

If you're working with a team, those issues happen sometimes if both you and others have made changes to the model or created migrations. Check this interesting MSDN article to get a few insights on how leverage those issues.