简体   繁体   中英

How do I create database backups using C#-WPF-Entity-Framework Code First application?

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.

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);
    }

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM