I was very encouraged by the fact that the average number of blog visits for two consecutive days was thousands! Let's continue with migration today.
First, let's look at a requirement. If we do not allow duplicate myblogs in the users table, how can we modify it?
Open the migrations folder and see what is missing below? After careful observation, it is not difficult to find that some class files consisting of timestamps and command strings entered yesterday are added,
For example, 201207181401353_adduser.cs is used to create a user. The Code is as follows:
Adduser
1 public partial class AddUser : DbMigration 2 { 3 public override void Up() 4 { 5 CreateTable( 6 "Users", 7 c => new 8 { 9 UserId = c.Int(nullable: false, identity: true),10 UserName = c.String(),11 MyBlog = c.String(),12 })13 .PrimaryKey(t => t.UserId).Index(t => t.UserName, unique: true);14 }15 16 public override void Down()17 {18 DropTable("Users");19 }20 }
We found that this up method is actually used to create a table, and we do not allow repeated myblog values in our requirements. In fact, it is used to create a unique constraint. The method is as follows:
1. since the classes in the migrations folder can be used to modify the database, you have to get it all out. Enter the "Add-migration addindex" command in the Package Manager Console and press Enter, then, in the migrations folder, you can see an additional 201207191412098_addindex.cs file.
2. open the file and see the following code:
Addindex
1 public partial class AddIndex : DbMigration 2 { 3 public override void Up() 4 { 5 6 } 7 8 public override void Down() 9 {10 11 }12 }
Based on the examples in the adduser class, we must add this constraint to the up () method and drop the constraint in the down () method. Therefore, we modify the Code as follows:
Addindex
1 public partial class AddIndex : DbMigration 2 { 3 public override void Up() 4 { 5 CreateIndex("Users", "MyBlog", unique: true); 6 } 7 8 public override void Down() 9 {10 DropIndex("Users", "MyBlog");11 }12 }
3. Enter the "Update-database-verbose" command in the Package Manager Console and press Enter!
Note: The premise is that there cannot be duplicate data in the database. Otherwise, this constraint cannot be created!
Next we will introduce a straightforward function. Execute SQL statements in migration to directly change the database, which is a solution for all diseases!
Suppose we want to add the user ID before the value of myblog. We can execute the SQL statement as follows:
1. Enter the "Add-migration setvalueofmyblog" command in the Package Manager Console and press Enter.
2. Find the generated setvalueofmyblog class and modify it as follows:
Setvalueofmyblog
1 public partial class SetValueOfMyBlog : DbMigration 2 { 3 public override void Up() 4 { 5 Sql("update Users set MyBlog=str(UserId)+'www.cnblogs'"); 6 } 7 8 public override void Down() 9 {10 }11 }
3. Enter the "Update-database-verbose" command in the Package Manager Console and press Enter!
Note: Each migration file can be executed only once with the update-database-verbose command. The migration file that has been executed will not be executed the next time the update-database-verbose command is executed, even if you modify it.
Codefirst can also restore the database to a specific version. If we want to add-migration setvalueofmyblog of revert, we need to use the 'Update-database-targetmigration: "setvalueofmyblog" command, it is not hard to find that the migration process is actually an inbound and elastic stack process. The elements in the stack are the migration files.
Hey, buddy, is resharper installed? If you install F12, check the dbmigration class to see how much migration can do. You can find out the source code of the dbmigration class. The only thing I feel a little disgusted with is that there is no comment in it, I don't want to take a look at it. I admit that many of its methods have good names, but the method parameters always need to be explained... This is because stylecopy in our company cannot run, and the Code cannot be checked in (I have to say that the Germans are very meticulous in doing things )... Suddenly I want to write something about clean code. I owe my friends in the garden and I will pay it back later!