0

I am using EF6 Code First with migrations in a new project. I have used this in a few projects already without issue.

Now this new project is against an existing database. I generated the standard Initial migration file, then deleted all the contents leaving just the Up() and Down() methods. This has worked for me in other projects, but not this time.

When I run update-database from the Package Manager Console, all works as expected.

PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
No pending explicit migrations.
Running Seed method.

Then I execute the migrations from code (as needed in production) and I get...

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 have been trying to get my head around this one for two days, and not getting anywhere. I have even added another migration file called "stub" and it is generated blank. EF does not see any changes to my model (of which there are none), so it has nothing to generate. Yet the migration execution via code persists with the error that there are migrations pending.

I have attached a logger to the code execution of the migrations and the output is this.

Target database is: 'FMS' (DataSource: (local)\SQL2012, Provider: System.Data.SqlClient, Origin: Configuration).
No pending explicit migrations.

And then I get the error message in my browser.

My configuration class

namespace FMS.Infrastructure.Repository.EF.Migrations.Stage
{
    public sealed class StageConfiguration : DbMigrationsConfiguration<StageDb>
    {
        public StageConfiguration()
        {
            AutomaticMigrationsEnabled = false;            
            MigrationsDirectory = @"Migrations\Stage";
            CommandTimeout = 3000;
            ContextKey = "FMS.Stage";
        }        
    }
}

And the code that performs the migrations

Database.SetInitializer(new MigrateDatabaseToLatestVersion<StageDb, StageConfiguration>());

var dbMigrator = new DbMigrator(new StageConfiguration());

var logger = new MigratorLoggingDecorator(dbMigrator, new DbMigrationLogger());

foreach (string migration in dbMigrator.GetPendingMigrations())
    Console.WriteLine(migration);

logger.Update();

I hope this is clear enough for those willing to try assist. If anyone has a tip, I am all ears. This is making me grey.

  • Could be there was something in the initial migration you need to apply but deleted out? Did you reverse engineer or manually create the classes and mappings? IAC, since you just started I would delete the migrations (folder and db), regenerate and examine the Up() code. BTW, you can use -IgnoreChanges to generate a migration to get things in sync. https://msdn.microsoft.com/en-us/data/dn579398.aspx#option1 – Steve Greene May 18 '15 at 15:57
  • I used the Add / New Item / ADO.NET Entity Data Model / Code First From Database wizard. Got the models I required, then deleted the generated file. I have started from scratch - no go. I also used the --IgnoreChanges when creating my Initial migration, but decided against it as I would rather keep the original code commented out as a backup of sorts (for a new DB generation). Yesterday I uncommented all the initial code, deleted the database, and kicked it off from scratch. It created the DB, executed the initial code, then failed with the good old "Unable to update database..." exception. – Michael Smit May 19 '15 at 14:44
  • To add to the above, if I run update-database from the Package Manager Console, it works 100%. No issues at all. I only get the error when running the migrations from code. – Michael Smit May 19 '15 at 14:50
  • Oh yes - and I have created the stub migration using add-migration with the --IgnoreChanges switch. Still works fine from the Package Manager Console, but breaks in code. – Michael Smit May 19 '15 at 14:52
  • Are your migrations in the same assembly? http://stackoverflow.com/questions/12353149/dbmigrator-getpendingmigrations-always-empty – Steve Greene May 19 '15 at 15:12
  • Migrations are in my Infrastructure.EF assembly. Models are in the Domain.Model assembly. update-database would not work from the package manager console if my structure was bad. I have just re-done the whole lot from scratch. Same issue. – Michael Smit May 19 '15 at 15:47
  • I went down to basics. Removed all models etc... Then created just the Users model. Updated the configuration file, and generated the Initial migration. update-database from the console is 100%. Executing from code breaks. The code execution is doing something different to the console update-database command. – Michael Smit May 19 '15 at 16:02
  • Tried another way... Initial migration file with up/down contents commented out. Get error after this executes. Enabled auto migrations, then analysed the logs. Example 1: Stock table with model Stock and annotation Table("Stock"). Auto migrations wants to rename this to Stocks. Example 2: Money type columns that got mapped as decimal and the annotation [Column(TypeName = "money")]. Auto migration wants to change it to Decimal(18,4) It is like EF has a mind of it's own, and is completely ignoring my models and type definitions. I can see what it wants to do now, but failing because .... – Michael Smit May 19 '15 at 16:38
  • ... auto migrations is off. If I try to generate a new migration file, it is empty. It does not pick up what auto migrations is doing when enabled. This quite frankly is a mess of note. – Michael Smit May 19 '15 at 16:39
  • 1
    I got it working. What a mess. In short, data annotations were being ignored completely. I had to use the fluent API to define column types money, text and image. All of these that were defined with a data annotation were causing the failure. The big mess was with auto migrations disabled, it worked in the console, but not code. I now have it working with auto migrations disabled, but updating the model using the fluent api and not data annotations. Thanks to all those that assisted along the way. – Michael Smit May 19 '15 at 17:24
  • I think I'm having exactly this same problem. Did you ever figure out why the Data Annotations weren't working without auto migrations? – LavaHot Feb 04 '16 at 22:36

0 Answers0