0

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?

2 Answers 2

1
+50

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

5 Comments

Where do i find the hidden migration files?
Its nothing extra, I meant hidden files in the project, there is a button in VS to show hidden files of the project.
Ive deleted all the migration files, added a new migration and rebuild the project and then updated the database and i still get the same error. How come i can add data to the database but when i try to get data it gives me this error? This is what im trying to do when i fails: var query = from r in _db.User select r.UserName;
Curious... these two things should not be connected. Which entity framework version do you use? And if it is a test project can you post the code somewhere so that I can take a look at it?
witch two things should not be connected? EF 5.0, it is a small project in the beginning.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.