我有一个应用程序,它使用EF和LocalDB作为数据库,由ClickOnce发布。这是我第一次使用LocalDB,我不知道如何为应用程序添加功能以编程方式备份/还原数据库。
ClickOnce安装的“我的应用路径”:
C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu..tion_939730333fb6fcc8_0001.0002_fd707bbb3c97f8d3
并且这是数据库文件的安装位置:
C:\Users\Mahdi Rashidi\AppData\Local\Apps\2.0\NOL11TLW.9XG\CZM702AQ.LPP\basu...exe_939730333fb6fcc8_0001.0002_none_8c555c3966727e7f
非常感谢 :)
这就是我对localDb进行备份和还原时所做的事情
public void BackupDatabase(string filePath) { using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString)) { string backupQuery = @"BACKUP DATABASE ""{0}"" TO DISK = N'{1}'"; backupQuery = string.Format(backupQuery, "full databsase file path like C:\tempDb.mdf", filePath); dbEntities.Database.SqlQuery<object>(backupQuery).ToList().FirstOrDefault(); } } public void RestoreDatabase(string filePath) { using (TVend2014Entities dbEntities = new TVend2014Entities(BaseData.ConnectionString)) { string restoreQuery = @"USE [Master]; ALTER DATABASE ""{0}"" SET SINGLE_USER WITH ROLLBACK IMMEDIATE; RESTORE DATABASE ""{0}"" FROM DISK='{1}' WITH REPLACE; ALTER DATABASE ""{0}"" SET MULTI_USER;"; restoreQuery = string.Format(restoreQuery, "full db file path", filePath); var list = dbEntities.Database.SqlQuery<object>(restoreQuery).ToList(); var resut = list.FirstOrDefault(); } }
希望这就是你想要的。