Reputation: 31
I'm developing a desktop application using entity-framework code first, I need to create backups on db some time by using a button click event. I'm using Entity-Framework version 6.1.3, Visual studio 2013 Ultimate.
Upvotes: 4
Views: 2955
Reputation: 6251
Although you do not need EF to do the back-up/restore this is a code snippet from here. Read the full article for details and requirements.
public static void BackupDatabase(string backUpFile)
{
ServerConnection con = new ServerConnection(@"xxxxx\SQLEXPRESS");
Server server = new Server(con);
Backup source = new Backup();
source.Action = BackupActionType.Database;
source.Database = "MyDataBaseName";
BackupDeviceItem destination = new BackupDeviceItem(backUpFile, DeviceType.File);
source.Devices.Add(destination);
source.SqlBackup(server);
con.Disconnect();
}
public static void RestoreDatabase(string backUpFile)
{
ServerConnection con = new ServerConnection(@"xxxxx\SQLEXPRESS");
Server server = new Server(con);
Restore destination = new Restore();
destination.Action = RestoreActionType.Database;
destination.Database = "MyDataBaseName";
BackupDeviceItem source = new BackupDeviceItem(backUpFile, DeviceType.File);
destination.Devices.Add(source);
destination.ReplaceDatabase = true;
destination.SqlRestore(server);
}
Upvotes: 3
Reputation: 2104
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: 0