5

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

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

4

3 回答 3

13

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

于 2012-12-19T06:33:21.477 回答
6

对于其他有此问题的朋友....使用
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
ref=> https://entityframework.codeplex.com/discussions/454994

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

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

于 2014-12-08T05:47:37.260 回答
0

这应该让你在恢复方面进行:

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 值:

RESTORE FILELISTONLY FROM DISK @backup_filename
于 2017-12-08T13:54:37.420 回答