MVC codefirst schema database migration steps


After building the basic class with code first mode, the project starts to build and run successfully, and the database table structure has been automatically generated.

However, I have new classes to add and fields to modify. What should I do? Delete the library and run? ha-ha

Using database migration, the original structure will not be changed, the new class will be used to create a separate table, or the existing database table, change the field, then modify the table.

Migration steps:

1. Open the package manager console: Tools > nuget package manager > package manager console

After clicking, the package manager console will pop up

The most important thing to note is the default project!!!

2. Start database migration and execute the command: enable migrations

Since models does not necessarily directly use the existing folder models in the project, when you select Start project as the MVC framework type, an error will be reported here

Specifically, the distribution of my current projects:

I separated the model so that when I selected search engine as the startup project by default, the first command I entered in the package manager console would report an error

Solution: select the default project and select the project where dbcontext is located. Here, my class library is

Successful operation will be prompted as follows:

In addition, the migrations directory is added under the dbcontext peer directory

3. Start database migration command

1. Add migration [custom version name]

Enter add migration update order header


Enter update database

Here, if you want to separate dbcontext as I did, you need to pay attention to adding the connection address of the database to the configuration file of the current class library

If it’s directly in the The dbcontext added to the models in the MVC framework can be read directly without any need Web.Config Connection string in file

Waiting for command execution

Something’s wrong

Start to search for SQL server configuration manage in my computer. If there is one, open it and find it

Configure TCP / IP protocol as enabled.

I don’t have this tool locally. I can only find it in the following way

Right click my computer, right click management, services and applications, SQL Server Configuration Manager

Reenter the command and wait for execution

Initial success

Because some tables in the database do not need to be changed

We can change the specific tables that need to be added or modified in the migrations directory

By modifying the createtable / droptable, you can control which tables need to be changed

namespace SAssassin.EF.Model.Migrations
  using System;
  using System.Data.Entity.Migrations;

  public partial class updateorderheader : DbMigration
    public override void Up()
        c => new
          Id = c.Int(nullable: false, identity: true),
          FileName = c.String(),
          FileDescription = c.String(),
          FilePath = c.String(),
          FileType = c.Byte(nullable: false),
          FileSize = c.String(),
          FileStatus = c.Byte(nullable: false),
          IsPublic = c.Byte(nullable: false),
          UserId = c.String(),
          UserName = c.String(),
          CreateDate = c.DateTime(nullable: false),
          LastModityDate = c.DateTime(nullable: false),
        .PrimaryKey(t => t.Id);

    public override void Down()

4. Modify the code configuration, the operation after implementation is not so troublesome

Modify the Configuration.cs File, start automatic migration

5. In the project Global.asax Application of_ Add the following line to start:

System.Data.Entity.Database.SetInitializer(new System.Data.Entity.MigrateDatabaseToLatestVersion<SAssassin.EF.Model.CodeFirst, SAssassin.EF.Model.Migrations.Configuration>());

At this point, the database migration has been completed.

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.