0

I'm trying to delete the created database, but I'm getting exception that the database is in use by this application. After executing this code, an exception is thrown: System.IO.IOException: The process cannot access the file because the file is being used by another process.

var context= new StorageDBcontext();
File.Delete(AppSettings.StorageDBPath()); //AppSettings.StorageDBPath() is path of the database
 public class StorageDBcontext : DbContext
    {
        public StorageDBcontext() :
            base(new SQLiteConnection()
            {
                ConnectionString = new SQLiteConnectionStringBuilder() { DataSource = AppSettings.StorageDBPath(), ForeignKeys = true }.ConnectionString
            }, true)
        {
            Database.Connection.Open();
            Database.Connection.Close();
        }
    }

I also tried to delete the database using this function, but I also got an exception: System.Data.Entity.Core.ProviderIncompatibleException: "DeleteDatabase is not supported by the provider."

var context= new StorageDBcontext();
context.Database.Delete();
  • Deleting db from code is not very common and not good practice . You can delete it manually . Maybe you can see this page https://stackoverflow.com/questions/16035333/how-to-delete-and-recreate-from-scratch-an-existing-ef-code-first-database (dbctx.Database.EnsureDeleted();) – firatt_ Mar 11 '23 at 10:48

0 Answers0