6

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.

0

3 Answers 3

3

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();
}
Sign up to request clarification or add additional context in comments.

5 Comments

Try with using using (var dbContext = new ApplicationDbContext())
Tied it but to no avail. DB is still there.
@memoryofadream I tested issue on my machine, see update, hope this time it works for you
Sorry for the long gap. I've been working on other stuff lately I believe I now understand the problem. The database is still being used by the current application, that's why I can't delete it. Seems like a chicken and egg problem. I've tried disposing the db context entirely and creating a new one, but didn't work. I'd like to remove the DB from the same process that created it and without having to restart sql. Otherwise I can delete it manually after the application closes without difficulty.
It is getting interesting. I did not try create and delete from application. As you report and from my attemps, operation can only be done with existing database...
1

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

Comments

0

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();
}

Comments

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.