Entity framework 意外删除了表,如何在不影响其它表的情况下恢复回来
关于EntityFramework数据迁移原理
查询数据库的表"__MigrationHistory",遍历代码库的Migrations文件夹下的所有文件,如果文件不在__MigrationHistory表内,那么就执行迁移。
有了上面的原理之后,我们来看一下如果我们不小心手动删除了一个表,如何在不影响其它表的情况下来恢复你删除的表:
方法一:
关于Model 以及 DBContext如下:
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; } public string Url { get; set; }
public virtual List<Post> Posts { get; set; }
} public class User
{
[Key]
public int UserId { get; set; }
public string Username { get; set; }
public string DisplayName { get; set; }
//public int? age { get; set; }
//public string interest { get; set; }
} public class School
{
public int SchoolId { get; set; } public string SchoolName { get; set; } public int SchoolLevel { get; set; }
} public class Teacher
{
[Key]
public int TecherId { get; set; }
public string TeacherName { get; set; }
} public class Post
{
public int PostId { get; set; }
public string Title { get; set; }
public string Content { get; set; } public int BlogId { get; set; }
public virtual Blog Blog { get; set; }
} public class Tutorial
{
[Key]
public int Id { get; set; } public int Name { get; set; }
} public class BloggingContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; } public DbSet<User> Users { get; set; } public DbSet<Tutorial> Tutorials { get; set; }
public DbSet<School> Schools { get; set; } public DbSet<Teacher> Teachers { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>().Property(u => u.DisplayName).HasColumnName("display_name");
modelBuilder.Entity<User>().Property(u => u.Username).HasColumnName("user_name");
}
}
migrations 文件夹下的文件如下:
对应数据库中的记录为:
假如此时我们手动删除了schools表,此时我们应该找到schools表的创建与修改的迁移code是在哪个Migrations文件夹下面的哪个文件,找到对应的文件
我们这里是在201503190341085_addmodels.cs中,
public partial class addmodels : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.Blogs",
c => new
{
BlogId = c.Int(nullable: false, identity: true),
Name = c.String(),
Url = c.String(),
})
.PrimaryKey(t => t.BlogId); CreateTable(
"dbo.Posts",
c => new
{
PostId = c.Int(nullable: false, identity: true),
Title = c.String(),
Content = c.String(),
BlogId = c.Int(nullable: false),
})
.PrimaryKey(t => t.PostId)
.ForeignKey("dbo.Blogs", t => t.BlogId, cascadeDelete: true)
.Index(t => t.BlogId); CreateTable(
"dbo.Schools",
c => new
{
SchoolId = c.Int(nullable: false, identity: true),
SchoolName = c.String(),
SchoolLevel = c.Int(nullable: false),
})
.PrimaryKey(t => t.SchoolId); CreateTable(
"dbo.Tutorials",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.Int(nullable: false),
})
.PrimaryKey(t => t.Id); CreateTable(
"dbo.Users",
c => new
{
UserId = c.Int(nullable: false, identity: true),
user_name = c.String(),
display_name = c.String(),
})
.PrimaryKey(t => t.UserId); } public override void Down()
{
DropForeignKey("dbo.Posts", "BlogId", "dbo.Blogs");
DropIndex("dbo.Posts", new[] { "BlogId" });
DropTable("dbo.Users");
DropTable("dbo.Tutorials");
DropTable("dbo.Schools");
DropTable("dbo.Posts");
DropTable("dbo.Blogs");
}
}
此时我们要做的只要把这个文件名201503190341085_addmodels.cs对应在数据迁移表中的记录删除,
delete __MigrationHistory where MigrationId = '201503190341085_addmodels' ,然后我们在201503190341085_addmodels.cs注释掉其它的迁移数据,只留下创建shools表的数据
public partial class addmodels : DbMigration
{
public override void Up()
{
//CreateTable(
// "dbo.Blogs",
// c => new
// {
// BlogId = c.Int(nullable: false, identity: true),
// Name = c.String(),
// Url = c.String(),
// })
// .PrimaryKey(t => t.BlogId); //CreateTable(
// "dbo.Posts",
// c => new
// {
// PostId = c.Int(nullable: false, identity: true),
// Title = c.String(),
// Content = c.String(),
// BlogId = c.Int(nullable: false),
// })
// .PrimaryKey(t => t.PostId)
// .ForeignKey("dbo.Blogs", t => t.BlogId, cascadeDelete: true)
// .Index(t => t.BlogId); CreateTable(
"dbo.Schools",
c => new
{
SchoolId = c.Int(nullable: false, identity: true),
SchoolName = c.String(),
SchoolLevel = c.Int(nullable: false),
})
.PrimaryKey(t => t.SchoolId); //CreateTable(
// "dbo.Tutorials",
// c => new
// {
// Id = c.Int(nullable: false, identity: true),
// Name = c.Int(nullable: false),
// })
// .PrimaryKey(t => t.Id); //CreateTable(
// "dbo.Users",
// c => new
// {
// UserId = c.Int(nullable: false, identity: true),
// user_name = c.String(),
// display_name = c.String(),
// })
// .PrimaryKey(t => t.UserId); } public override void Down()
{
//DropForeignKey("dbo.Posts", "BlogId", "dbo.Blogs");
//DropIndex("dbo.Posts", new[] { "BlogId" });
//DropTable("dbo.Users");
//DropTable("dbo.Tutorials");
DropTable("dbo.Schools");
//DropTable("dbo.Posts");
//DropTable("dbo.Blogs");
}
}
此时如果你在package-manager console 中执行update-database 即可完成schools表的恢复
方法二:
利用 Update-Database -Script -SourceMigration $InitialDatabase (这是一个变量不需要你去改成你自己的数据库名字) 或者
Update-Database -Script -SourceMigration Second -TargetMigration First (用这两个中的哪个是视情况而定的)
(Second First 就是你的migrations文件夹下的文件名字的后面那个 比喻 201503190906406_addmodels 你就输入 Update-Database -Script -SourceMigration addmodels)
这样会产生一个脚本文件,你在那个脚本文件中找出你恢复的表的一些创建或者修改信息
我找到了Schools的创建SQL , 在数据库中执行即可:
CREATE TABLE [dbo].[Schools] (
[SchoolId] [int] NOT NULL IDENTITY,
[SchoolName] [nvarchar](max),
[SchoolLevel] [int] NOT NULL,
CONSTRAINT [PK_dbo.Schools] PRIMARY KEY ([SchoolId])
)
这样你就恢复好了
Entity framework 意外删除了表,如何在不影响其它表的情况下恢复回来的更多相关文章
- mvc+entity framework database first,生成的model每次更新一个表会更新所有的model
在使用Entity Framework 的Database frist或model first时,直接加attribute到modle类上是太现实也不合理的,因为model类是自动生成的,重新生成后会 ...
- Oracle备份恢复之无备份情况下恢复undo表空间
UNDO表空间存储着DML操作数据块的前镜像数据,在数据回滚,一致性读,闪回操作,实例恢复的时候都可能用到UNDO表空间中的数据.如果在生产过程中丢失或破坏了UNDO表空间,可能导致某些事务无法回滚, ...
- 在注册表中无Python3.5安装路径的情况下安装pywin32-
当安装pywin32出现Python Version 3.5 required which was not found in the registry的时候表面注册表中没有Python3.5的安装路径 ...
- 在docker容器下利用数据卷实现在删除了mysql容器或者镜像的情况下恢复数据
当把mysql容器销毁,在新建一个容器,进行之前的数据恢复. 因为之前建立了数据卷,那么现在就可以利用这个数据卷进行数据恢复. 使用docker volume create volume_name命令 ...
- Entity Framework 5.0系列之自动生成Code First代码
在前面的文章中我们提到Entity Framework的"Code First"模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework P ...
- 【转】Entity Framework 5.0系列之自动生成Code First代码
在前面的文章中我们提到Entity Framework的“Code First”模式也同样可以基于现有数据库进行开发.今天就让我们一起看一下使用Entity Framework Power Tools ...
- (转)Entity Framework 5.0系列之自动生成Code First代码
原文地址:http://www.cnblogs.com/kenshincui/archive/2013/08/29/3290527.html 在前面的文章中我们提到Entity Framework的“ ...
- 让Entity Framework启动不再效验__MigrationHistory表
Entity Framework中DbContext首次加载OnModelCreating会检查__MigrationHistory表,作为使用Code Frist编程模式,而实际先有数据库时,这种检 ...
- Entity Framework Code First关系映射约定
本篇随笔目录: 1.外键列名默认约定 2.一对多关系 3.一对一关系 4.多对多关系 5.一对多自反关系 6.多对多自反关系 在关系数据库中,不同表之间往往不是全部都单独存在,而是相互存在关联的.两个 ...
随机推荐
- mybatis bind标签
开门见山的说,平时写模糊查询,一直用${name},例如: select * from table where name like '%${name}%' 后来知道了,这样写可能会引发sql注入,于是 ...
- github免费私有仓库使用
本文链接:https://blog.csdn.net/subfate/article/details/86147645github仓库前不久开放了个人私有仓库(原来要收费),个人使用无数量限制.对于想 ...
- 123457123457#0#-----com.threeapp.renzhepaoku01----儿童跑酷游戏(忍者版)
com.threeapp.renzhepaoku01----儿童跑酷游戏(忍者版)
- 配置Apache运行在event事件驱动模式下
(1)启用MPM Include conf/extra/httpd-mpm.conf (2)配置evnet MPM参数 <IfModule event.c> #default 3 Ser ...
- PostgreSQL学习笔记——内置函数
算术函数(数值计算) +(加).-(减).*(乘)./(除) ABS函数--绝对值: ABS(数值) MOD--求余: MOD(被除数,除数) ROUND--四舍五入: ROUND(对象数值,保留小数 ...
- pipeline代码自动生成
如图所示,安装完插件后,Sample Step里就有相应的选项,选择某个选项后,点击Generate Pipeline Script按钮,就可以自动生成代码片段,然后放入pipeline流水线里就可以 ...
- 单点登录系统(SSO)详细设计说明书(上篇)
1.引言 1.1编写目的 为了单点登录系统(SSO系统)的可行性,完整性,并能按照预期的设想实现该系统,特编写需求说明书. 同时,说明书也发挥与策划和设计人员更好地沟通的作用. 1.2 ...
- 机器学习第一章——NFL的个人理解
第一篇博客,想给自己的学习加深记忆.看到书中第一个公式时,本来想直接看证明结果就好,然鹅...作者在备注上写:这里只用到一些非常基础的数学知识,只准备读第一章且有“数学恐惧”的读者可跳过...嘤嘤嘤, ...
- VBA实现打开Excel文件读取内容拷贝Format且加超链接
'-------------------一覧取得----------------------------- Sub getRedmineGrid_Click() Dim wb As Workbook ...
- 部署Harbor私有镜像仓库
Harbor私有镜像仓库无坑搭建 目录 1. harbor介绍 2. docker-ce的安装 3. docker-compose的安装 4. Harbor私有仓库的安装 5. 客户端连接镜像仓库配置 ...