备份数据库mdf&实体框架 [英] Backup a database mdf & Entity Framework

查看:105
本文介绍了备份数据库mdf&实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要使用实体框架的数据库(mdf文件).是否可以对MDF文件进行备份. 我已经尝试过但是SMO,但是问题是因为我使用的是mdf文件,数据库名称为空.我已经知道它是自动生成的.

I have a database (mdf file) wich I'm approaching with the Entity Framework. Is it possible to make a backup of the MDF file. I tried already but SMO but the problem is because I'm using a mdf file the database name is empty. I've read that it's autogenerated.

我的备用验证码部分:

        String destinationPath = "C:\\";
        Backup sqlBackup = new Backup();

        sqlBackup.Action = BackupActionType.Database;
        sqlBackup.BackupSetDescription = "ArchiveDataBase:" + DateTime.Now.ToShortDateString();
        sqlBackup.BackupSetName = "Archive";

        BackupDeviceItem deviceItem = new BackupDeviceItem(destinationPath, DeviceType.File);
        ServerConnection connection = new ServerConnection(".\\SQLEXPRESS");
        Server sqlServer = new Server(connection);
        StringCollection sc = new StringCollection();
        sc.Add(Environment.CurrentDirectory + "\\db\\Xmain.mdf"); //Bin directory
        sc.Add(Environment.CurrentDirectory + "\\db\\Xmain_log.ldf");
        sqlServer.AttachDatabase("Xmain", sc);
        Database db = sqlServer.Databases["Xmain"];
        sqlBackup.Initialize = true;
        sqlBackup.Checksum = true;
        sqlBackup.ContinueAfterError = true;

        sqlBackup.Devices.Add(deviceItem);
        sqlBackup.Incremental = false;

        sqlBackup.ExpirationDate = DateTime.Now.AddDays(3);
        sqlBackup.LogTruncation = BackupTruncateLogType.Truncate;

        sqlBackup.FormatMedia = false;

        sqlBackup.SqlBackup(sqlServer);

推荐答案

我在这里遗漏了一些东西,需要更多上下文,但我会花一秒钟的时间,看看是否有帮助.

I am missing something here and need more context, but I will rant on for a second and see if anything is helpful.

您是说实际上是在备份文件,而不是数据吗?如果是这样,简单的答案是否".问题在于,当SQL Server将文件附加到数据库服务器(在这种情况下为SQL Express)时,它将锁定该文件.您可以分离并复制然后附加,但是在此期间该应用程序将关闭.也可以手动完成.

Do you mean actually back up the file, not the data? If so, the easy answer is no. The problem is SQL Server will lock the file when it is attached to the database server (SQL Express in this case). You can detatch and copy and then attach, but the application will be down during that time. This can also be done manually.

如果您要备份数据,我将考虑在SQL Server中而不是通过编程来安排数据,除非您不能那样做.备份不仅仅是程序的一部分,更是一种维护功能.

If you want to backup the data, I would consider scheduling it within SQL Server rather than programmatic, unless you cannot do it that way. Backup is more of a maintenance function than a part of the program.

对于您的数据库名称为空,这是不可能的.实际上,您似乎正在尝试建立一个名为XMain的数据库.

As for your database name being empty, that is impossible. In fact, it looks like you are trying to set up a database called XMain.

这篇关于备份数据库mdf&实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆