Entity framework in the case recover back accidentally deleted the table, how without affecting other tables

 About EntityFramework principle data migration

Table query the database "__MigrationHistory", all files in the codebase Migrations folder, if the file is not __MigrationHistory table inside, then perform the migration.

With the above principle After that, we look at if we are not careful to manually delete a table, how the table without affecting the other down the table you recover deleted:

 

method one:

About Model and DBContext as follows:

   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");
        }
    }

  

File migrations folder as follows:

In the corresponding database record is:

If the schools at this time we manually delete the table, then we should find the schools to create and modify tables migration code is which of the following documents which Migrations files in the folder, find the corresponding file

We are here in 201503190341085_addmodels.cs in

   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");
         }
     }

  

At this time, as long as the file name 201503190341085_addmodels.cs the corresponding data in the migration table to delete records we have to do,

  delete __MigrationHistory where MigrationId = '201503190341085_addmodels', and then we commented other migration data in 201503190341085_addmodels.cs, leaving only create a table of data 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");
        }
    }

  

At this point if you run update-database in the package-manager console in the table to complete the restoration schools

 

Method Two:

Using the Update-Database -Script -SourceMigration $ InitialDatabase (which is a variable that does not require you to go into your own database name) or  

Update-Database -Script -SourceMigration Second -TargetMigration First ( both of which use is, as the case may be) 
(First Second is appended to the file name of your migrations folder metaphor 201503190906406_addmodels that you enter the Update-Database -Script -SourceMigration addmodels)
This creates a script file, you find your table recovery in some of the script file is created or modified information

I found the Schools of creation SQL, you can perform in the database:

CREATE TABLE [dbo].[Schools] (
[SchoolId] [int] NOT NULL IDENTITY,
[SchoolName] [nvarchar](max),
[SchoolLevel] [int] NOT NULL,
CONSTRAINT [PK_dbo.Schools] PRIMARY KEY ([SchoolId])
)

  


So you better recovery

 

Guess you like

Origin www.cnblogs.com/QueryWord/p/11607723.html