Daniel Gustafsson
Daniel Gustafsson

Reputation: 1817

model changed, update database using codefirst

Im using code-first to create a database. And now im trying to make a linq expression to get data out of the database but then i get this error:

"The model backing the 'FantasySport' context has changed since the database was created. Consider using Code First Migrations to update the database"

So i go to package-manager console and types update-database and it says that there is nothing to update.

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

----EDIT------

Here is the initializer method:

  private class SimpleMembershipInitializer
    {
        public SimpleMembershipInitializer()
        {
            Database.SetInitializer<UsersContext>(null);

            try
            {
                using (var context = new UsersContext())
                {
                    if (!context.Database.Exists())
                    {
                        // Create the SimpleMembership database without Entity Framework migration schema
                        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
                    }
                }

                WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex);
            }
        }

So what is the problem?

Upvotes: 0

Views: 576

Answers (2)

user3083619
user3083619

Reputation:

You first have to add a migration to update to. You can do it by typing the following command in the package manager console:

add-migration "a custom name for your migration here..."

This will cause a migration to be created, after which you can run the Update-database command from the package manager console.

[EDIT] The Database.SetInitializer method specifies the strategy for creating and seeding your database on the fly. Since that is what is causing this error, and since we are manually creating and seeding your database through the update-database command, we want to turn it off. http://www.codeguru.com/csharp/article.php/c19999/Understanding-Database-Initializers-in-Entity-Framework-Code-First.htm

Upvotes: 1

To be able to update the database through migrations you have to use the

add-migration "<migration-name>"

command however I've experienced it getting "stuck" sometimes meaning that you will get this error or it will display the usual message that there is a need for another migration step. To solve this make sure that

  • there is no hidden migration file which was not deleted just removed from the project
  • always build the project after an add-migration command before issuing the update-database command
  • make sure the connection string to the database is the same in the project you issue the update-database command points to the same database your program uses.

Other than this you can check whether the __MigrationHistory table contains all the migration steps including the newest as the Entity Framework tests the current model against this table's records.

Some of these voodoo (like rebuilding and cleaning) was only required around EF 4.4 I guess the update-database script was improved in this area as well.

Upvotes: 0

Related Questions