【问题标题】:Restore and Backup with Entity Framework使用实体框架进行还原和备份
【发布时间】:2012-12-19 06:14:26
【问题描述】:

我在一个项目中使用 C#、.net 4、Entity Framework 和 SQL Server 2008 R2。

我不熟悉 Entity Framework 从数据库的备份和恢复。请帮我在实体框架中编写恢复和备份代码

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    实体框架是一个 ORM(对象关系映射器),旨在处理与单个实体和/或实体短列表的交互。它既不是为批量操作而设计的,也不是服务器管理框架。所以不 - 我认为你不能使用实体框架来做到这一点 - 这不是它的工作。

    为工作使用合适的工具!使用 SQL Server Management Studio 处理备份/恢复 - 或者如果您必须以编程方式执行此操作,请使用专门用于此类作业的 SMO(服务器管理对象)

    【讨论】:

      【解决方案2】:

      致其他有此问题的朋友....
      使用 ExecuteSqlCommand 可以备份 EF 6+ 中的数据库。
      例如:(此代码创建您的数据库的备份,我已经对此进行了测试。)

      string dbname = db.Database.Connection.Database;
      string sqlCommand = @"BACKUP DATABASE [{0}] TO  DISK = N'{1}' WITH NOFORMAT, NOINIT,  NAME = N'MyAir-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10";
      db.Database.ExecuteSqlCommand(System.Data.Entity.TransactionalBehavior.DoNotEnsureTransaction, string.Format(sqlCommand,dbname, "Amin9999999999999"));
      

      备份保存在 C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Backup
      参考=> https://entityframework.codeplex.com/discussions/454994

      但我不建议使用这种方法!

      我强烈推荐使用下面这篇文章:
      http://www.c-sharpcorner.com/Blogs/8679/backup-and-restore-the-database-in-Asp-Net-web-application.aspx

      【讨论】:

        【解决方案3】:

        这应该让你继续恢复:

        void LoadDB(
            System.Data.Entity.DbContext context,
            string backup_filename,
            string orig_mdf, // the original LogicalName name of the data (also called the MDF) file within the backup file
            string orig_ldf, // the original LogicalName name of the log (also called the LDF) file within the backup file
            string new_database_name
        )
        {
            var database_dir = System.IO.Path.GetTempPath();
            var temp_mdf = $"{database_dir}{new_database_name}.mdf";
            var temp_ldf = $"{database_dir}{new_database_name}.ldf";
            var query = @"RESTORE DATABASE @new_database_name FROM DISK = @backup_filename
                WITH MOVE @orig_mdf TO @temp_mdf,
                MOVE @orig_ldf TO  @temp_ldf,
                REPLACE;";
            context.Database.ExecuteSqlCommand(
                // Do not use a transaction for this query so we can load without getting an exception:
                // "cannot perform a backup or restore operation within a transaction"
                TransactionalBehavior.DoNotEnsureTransaction,
                query,
                new[] {
                new SqlParameter("@backup_filename", backup_filename),
                new SqlParameter("@database_dir", database_dir),
                new SqlParameter("@new_database_name", new_database_name),
                new SqlParameter("@orig_mdf", orig_mdf),
                new SqlParameter("@orig_ldf", orig_ldf),
                new SqlParameter("@temp_mdf", temp_mdf),
                new SqlParameter("@temp_ldf", temp_ldf),
                }
            );
        }
        

        如果你事先不知道,MDF 和 LDF LogicalName 值可以通过类似这样的查询获得manuallyprogrammatically

        RESTORE FILELISTONLY FROM DISK @backup_filename
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-04-23
          • 2023-04-09
          • 2015-11-22
          • 1970-01-01
          • 2023-04-08
          • 2012-02-29
          • 1970-01-01
          • 2015-10-30
          相关资源
          最近更新 更多