memory of a dream
memory of a dream

Reputation: 1267

Drop a database using entity framework

I'm trying to remove a database form my application using entity framework. The code I use is the following:

using (var dbContext = container.Resolve<ApplicationDbContext>())
    {
      dbContext.Database.Delete();
    }

According to msdn this should work but nothing happens.

the dbContext is registered using ContainerControlledLifetimeManager and should be the same instance used to create the DB.

Upvotes: 6

Views: 15251

Answers (3)

Sayid Yarmohammadi
Sayid Yarmohammadi

Reputation: 1

All of your changes occurred on local variable dbcontext.

That is final kick > add dbContext.SaveChanges(); at the end of your using block like so:

 using (var dbContext = container.Resolve<ApplicationDbContext>())
{
  dbContext.Database.Delete();
  dbContext.SaveChanges();
}

Upvotes: 0

Bruno Oliveira
Bruno Oliveira

Reputation: 85

After @moguzalp and this (MSDN Database.Delete Function), I came with a solution for my case:

using System.Data.Entity;

Database.Delete("connectionStringOrName");

In my case I was trying to Recreate a mssqllocalDb database for test purposes. But whenever I used the same DbContext (or an immediately new one, disposing the first and opening another), it looked like the database was still up when I tried to create it.

Bad Example: (x)

public static void RecreateDatabase(string schemaScript)
{   
    using (DbContext context = new DbContext("connectionStringName"))
    {
        context.Database.Delete(); // Delete returns true, but...
        Database database = context.Database;
        database.Create(); // Database already exists!
        database.ExecuteSqlCommand(schemaScript);
    }
}

Working example: (/)

public static void RecreateDatabase(string schemaScript)
{   
    Database.Delete(); // Opens and disposes its own connection

    using (DbContext context = new DbContext("connectionStringName")) // New connection
    {
        Database database = context.Database;
        database.Create(); // Works!
        database.ExecuteSqlCommand(schemaScript);
    }
}

Context: I'm using this on an [AssemblyInitialize] function to test my ModelContexts

Upvotes: 1

asdf_enel_hak
asdf_enel_hak

Reputation: 7630

Adding, updating and deleting instances of entity types needs dbContext.SaveChanges() to reflect changes.

However dbContext.Database.Delete() does not need dbContext.SaveChanges().

If you open connection for example from Sql Management Studio to your database and try to dbContext.Database.Delete() then you receive Cannot drop database "dbContext" because it is currently in use. If you restart you sql server, you drop those connections and then retry dbContext.Database.Delete() you successfully drop database.

Last thing is refresh database list in Sql Management Studio in order to see that database is not there any more.

Testing with this code snippet:

using (var dbContext = new dbContext())
{
    dbContext.Database.Delete();
}

Upvotes: 3

Related Questions