2

I have a solution with two projects, the main has the user classes (the default of MVC5) and the other one I have my business classes.

After a few changes in my code I've got the error:

The model backing the 'EfDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).

I've executed the commands for add a new migration and for update the database. The error still goes on.

After this I tried many things to solve the problem, including these solutions:

https://stackoverflow.com/a/22451879/2394172

and

https://stackoverflow.com/a/11679386/2394172

But I still have this error:

enter image description here

PS: I already deleted my database and recreate it and the problem still on.

I can't figure out why this error still happening. When I execute the command Add-Migration the file created has the functions blank.

UPDATE (12/11/2014):

I've made a video showing all the process that I'm trying, proving that I'm using the same database I was updating with migrations.

http://youtu.be/hNVG10NynZU

By the way, I notice that I can insert data in the database, as shown in the video, but after it, the error occurs.

Community
  • 1
  • 1
Wellington Zanelli
  • 1,894
  • 3
  • 18
  • 43
  • 1
    Have you tried migrating even with the blank file? Sometimes doing that just gives EF a little kick to start working again. – Corey Adler Nov 07 '14 at 15:17
  • For sure I already did this. – Wellington Zanelli Nov 07 '14 at 16:05
  • did you configure migrations from package manager or using fluid api `OnModelCreating`? – Dave Alperovich Dec 03 '14 at 15:04
  • @DaveAlperovich I used the ''OnModelCreating'' to remove the automatic pluralizing. And in the nugget package manager I only run the command Enable-Migrations. – Wellington Zanelli Dec 04 '14 at 17:50
  • 1
    Setting migrations from Nuget is the recommended way, but I find it ambiguous and buggy. Try setting migrations in the models base class: `public ResumeContext() : base("name=ResumeContext") { this.Configuration.LazyLoadingEnabled = false; this.Configuration.ProxyCreationEnabled = false; this.Configuration.AutoDetectChangesEnabled = true; Database.SetInitializer( new MigrateDatabaseToLatestVersion()); }` – Dave Alperovich Dec 04 '14 at 18:45
  • `class ResumeMigrationsConfiguration : DbMigrationsConfiguration { public ResumeMigrationsConfiguration() { this.AutomaticMigrationDataLossAllowed = true; this.AutomaticMigrationsEnabled = true; } }` – Dave Alperovich Dec 04 '14 at 18:48

3 Answers3

1

If you care nothing for your existing data, the following is sure to work:

  1. Throw away your database
  2. Throw away all the migration files
  3. Run add-migration
  4. Run update-database

It's a bit of a steamroller, but it will get you out of trouble if you're not interested in finding out what the trouble actually is.

Martijn
  • 11,964
  • 12
  • 50
  • 96
1

Your code is failing on an inner call to CreateDatabaseIfNotExists...InitializeDatabase - for some reason, your application thinks the database doesn't exist and is trying to create it from your model code. You are using the same database for the ASPNet Identity tables and also your proprietary tables; the first time you try to access data from your proprietary table (Cms), you get the exception.

Perhaps use a different database name for the ASPNet tables so they're not in the CMS database.

Or, since you are using migrations in the Package Manager Console to create/update your schema, don't use the CodeFirst Database Initializer functionality. In your EfDbContext's Constructor, pass in null to the Database.SetInitializer<>() method to prevent attempts to initialize the database upon first access. See this link for code sample:

http://www.entityframeworktutorial.net/code-first/turn-off-database-initialization-in-code-first.aspx.

1

Not sure but Try using -projectName when you enable migrations or you addmigrations. I remember having some problems when running migrations with multiple projects in solution. I remeber reading somewhere that they use the Set as StartUp project or somethink like that.

Nikatlas
  • 184
  • 13