How to back up a database using the C # -WPF-Entity-Framework Code First application?

I am developing a desktop application using entity-framework code first, I need to backup db for some time using the button click event. I am using Entity-Framework version 6.1.3, Visual Studio 2013 Ultimate.

+4
source share
2 answers

Although you don't need EF for backup / restore, this is a snippet of code 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);
    }
+3
source

Entity Framework - - ORM - / . . - , Entity Framework -

! SQL Server Management Studio /, , SMO ( ), .

0

All Articles