Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 377
Ahmad Kazemi Avatar asked Dec 19 '12 06:12

Ahmad Kazemi


2 Answers

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

like image 97
hamid reza mansouri Avatar answered Oct 09 '22 08:10

hamid reza mansouri


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

like image 29
Amin Ghaderi Avatar answered Oct 09 '22 10:10

Amin Ghaderi