Ahmad Kazemi
Ahmad Kazemi

Reputation: 141

Restore and Backup with Entity Framework

I use C#, .net 4, Entity Framework and SQL Server 2008 R2 in a project.

I have no familiarity with backup and restore from database by Entity Framework. Please help me to write restore and backup code in Entity Framework

Upvotes: 6

Views: 28210

Answers (3)

John Cummings
John Cummings

Reputation: 2184

This should get you going on the restore side:

void LoadDB(
    System.Data.Entity.DbContext context,
    string backup_filename,
    string orig_mdf, // the original LogicalName name of the data (also called the MDF) file within the backup file
    string orig_ldf, // the original LogicalName name of the log (also called the LDF) file within the backup file
    string new_database_name
)
{
    var database_dir = System.IO.Path.GetTempPath();
    var temp_mdf = $"{database_dir}{new_database_name}.mdf";
    var temp_ldf = $"{database_dir}{new_database_name}.ldf";
    var query = @"RESTORE DATABASE @new_database_name FROM DISK = @backup_filename
        WITH MOVE @orig_mdf TO @temp_mdf,
        MOVE @orig_ldf TO  @temp_ldf,
        REPLACE;";
    context.Database.ExecuteSqlCommand(
        // Do not use a transaction for this query so we can load without getting an exception:
        // "cannot perform a backup or restore operation within a transaction"
        TransactionalBehavior.DoNotEnsureTransaction,
        query,
        new[] {
        new SqlParameter("@backup_filename", backup_filename),
        new SqlParameter("@database_dir", database_dir),
        new SqlParameter("@new_database_name", new_database_name),
        new SqlParameter("@orig_mdf", orig_mdf),
        new SqlParameter("@orig_ldf", orig_ldf),
        new SqlParameter("@temp_mdf", temp_mdf),
        new SqlParameter("@temp_ldf", temp_ldf),
        }
    );
}

If you don't know them beforehand, the MDF and LDF LogicalName values can be obtained manually or programmatically from a query like this one:

RESTORE FILELISTONLY FROM DISK @backup_filename

Upvotes: 0

Amin Ghaderi
Amin Ghaderi

Reputation: 1036

To other friends who have this problem ....
Useing ExecuteSqlCommand can backup of db in EF 6+ .
For example : (this code create backup of your DB , I had tested this.)

string dbname = db.Database.Connection.Database;
string sqlCommand = @"BACKUP DATABASE [{0}] TO  DISK = N'{1}' WITH NOFORMAT, NOINIT,  NAME = N'MyAir-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand,dbname, "Amin9999999999999"));

backup saved in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup
ref=> https://entityframework.codeplex.com/discussions/454994

but I do not recommend for working with this method!

I strongly recommend the use of the article below:
http://www.c-sharpcorner.com/Blogs/8679/backup-and-restore-the-database-in-Asp-Net-web-application.aspx

Upvotes: 7

hamid reza mansouri
hamid reza mansouri

Reputation: 11343

Entity Framework is an ORM - object-relational mapper - designed to handle interactions with single entities and/or short lists of entities. It's neither designed for bulk operations, nor is it a server admin framework. So no - I don't think you can do this using Entity Framework - that's not its job.

Use an appropriate tool for the job! Either use SQL Server Management Studio to handle backup/restore - or if you must do it programmatically, use the SMO (Server Management Objects) which is intended for exactly these kinds of jobs

Upvotes: 13

Related Questions