数据库
首页 > 数据库> > 如何使用C#-WPF-Entity-Framework Code First应用程序创建数据库备份?

如何使用C#-WPF-Entity-Framework Code First应用程序创建数据库备份?

作者:互联网

我首先使用实体​​框架代码开发桌面应用程序,我需要通过使用按钮单击事件在db上创建备份.我正在使用Entity-Framework版本6.1.3,Visual Studio 2013 Ultimate.

解决方法:

虽然您不需要EF来进行备份/恢复,但这是here的代码片段.阅读完整文章了解详细信息和要求.

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

标签:c,entity-framework,ef-code-first,code-first
来源: https://codeday.me/bug/20190628/1317371.html