Make a copy of the entity dbcontext files -


i'm using entity dbcontext act project data storage. copy .mdf , .ldf within program. (saveas command) can't copy files cause file in use error.

can detach database, copy files (.mdf, .ldf), reattach database files.

after looking around pieced together, else may interested

    /// <summary> detach database localdb. must done prior deleting it. must done after inadvertent (or ill advised) manual delete.</summary>     /// <param name="dbname">the name of database, not filename.</param>     /// <param name="directory"></param>     /// <param name="newname"></param>     /// <param name="newdirectory"></param>     /// <remarks></remarks>     public static void copydatabase(string dbname, string directory, string newname = null, string newdirectory = null)     {         string dbfilename = null;         // connect master database in order excute detach command on         sqlconnectionstringbuilder builder = new sqlconnectionstringbuilder()                                              {                                                  datasource = @"(localdb)\v11.0",                                                  initialcatalog = "master",                                                  integratedsecurity = true                                              };         using (sqlconnection connection = new sqlconnection(builder.tostring()))         {             try             {                 connection.open();                  datatable databases = connection.getschema("databases");                 foreach (datarow database in databases.rows)                 {                     string databasename = database.field<string>("database_name");                     short dbid = database.field<short>("dbid");                     datetime creationdate = database.field<datetime>("create_date");                     if (databasename.startswith(dbname, true, null) || databasename.endswith(dbname, true, null) || databasename.endswith(dbname + ".mdf", true, null))                     {                         dbfilename = databasename;                         break;                     }                  }                 if (string.isnullorempty(dbfilename)) return;                   var cmd = connection.createcommand();                 // before database file can detached code workaround below has applied.                 // http://gunnalag.wordpress.com/2012/02/27/fix-cannot-detach-the-database-dbname-because-it-is-currently-in-use-microsoft-sql-server-error-3703/                 //cmd.commandtext = string.format("select [name] sys.databases d d.database_id > 4");                  cmd.commandtext = string.format("alter database [{0}] set offline rollback immediate", dbfilename);                 cmd.executenonquery();                 try                 {                     string filename = path.combine(directory, dbname + ".mdf");                     file.copy(filename, path.combine(newdirectory ?? directory, dbname ?? newname + ".pdk"), true);                     filename = path.combine(directory, dbname + "_log.ldf");                     file.copy(filename, path.combine(newdirectory ?? directory, dbname ?? newname + "_log.lpdf"), true);                 }                 catch (exception ex)                 {                     helpers.debuglogger.caught(ex, "copydatabase file copying failed");                 }                                 {                     cmd.commandtext = string.format("alter database [{0}] set online", dbfilename);                     cmd.executenonquery();                 }             }             catch (exception ex)             {                 helpers.debuglogger.caught(ex, "copydatabase altering database failed");             }         }      } 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

IIS->Tomcat Redirect: multiple worker with default -