0

Every time I do update-database -v -f I want my database to be fully dropped and then created empty again. Is it possible to do?

I tried to delete it and then create it but it doesn't work. Strange thing that I did exactly same thing in EF4 and it did work, but now it does not.

So how can I re-create my database using some command?

Migrations is enabled for context 'MainDataContext' but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the 'Update-Database' command from the Package Manager Console.

This is my seed method

protected override void Seed(Api.Models.MainDataContext context)
{
    context.Database.Delete();
    context.Database.Create();
    // context.Database.Initialize(true); Also tried this but same result
}

Note: Truncate with raw queries does not work, too many relations.
Note: Delete all objects is bad idea, too many objects.

3 Answers 3

2

Could you perhaps first run the Update-Database like so:

Update-Database -TargetMigration:0

That would roll all migrations back to zero. And then run Update-Database again.

Edit: The seed method should not do anything to database structure, it should only modify the data. Migrations should be used instead.

Edit2: I can't find information on deleting the database with Update-Database, it will only rollback until first migration, which you seem to have none. Only when deploying it through code, you can use DropCreateDatabaseAlways-strategy like so: http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-first.aspx If you will do the migration manually, you'll probably have to manually remove the database before running Update-Database.

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

2 Comments

Strange, it gives me Target database is already at version 0.
Try putting a specific target name instead to rollback: Update-Database -TargetMigration:MyMigration
0

Do you want to drop and recreate the database when the model changes? How about...

Database.SetInitializer(new DropCreateDatabaseIfModelChanges());

You are right, EF6 has changed the way migrations are handled.

You will have to delete your Migrations folder and all the classes in there.

@cederlof is right about update-database: there is not a drop database switch, or a create database switch. The only wan (AFAIK!) to create a EF6 database is to do new CloudMinderDataContext(someconnectionstring)

I had a problem because I wanted migrations to update my staging database but could not then create a new database in code. This was fine in EF4 but not in EF6. I had to use SQL to create database, then migrate it...

How do I generate an EF6 database with migrations enabled, without using update-database?

and

http://softwaremechanik.wordpress.com/2013/11/04/ef6-if-migrations-are-enabled-cannot-createdatabaseifnotexists/

1 Comment

Yeah that I know, but what if my models aren't changing at all. I just want to drop it and recreate. Reason is intensive testing.
0

Hi Steve: To drop one database use this. If you are integration testing your tests might actually run in parallel so be sure you've finished with this database before calling this...

    private void DropDatabase(string connectionString)
    {
        var bld = new SqlConnectionStringBuilder(connectionString);

        var dbName = bld.InitialCatalog; //database you want to drop

        bld.InitialCatalog = "master";

        var masterConnectionString = bld.ConnectionString;

        using (var cnn = new SqlConnection(masterConnectionString))
        {

            using (var cmd = new System.Data.SqlClient.SqlCommand())
            {
                cmd.Connection = cnn;
                cnn.Open();
                cmd.CommandText = string.Format("ALTER DATABASE {0} SET  SINGLE_USER WITH ROLLBACK IMMEDIATE",
                              dbName);
                cmd.ExecuteNonQuery();
                cmd.CommandText = string.Format("DROP DATABASE {0}",
                              dbName);
                cmd.ExecuteNonQuery();
            }
        }             
    }

1 Comment

I'm actually creating one database per test and using ClassCleanup to zap them all at the end... softwaremechanik.wordpress.com/2013/11/26/…

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.