Asp.net mvc CodeFirst mode database migration steps, mvccodefirst

Source: Internet
Author: User

Asp.net mvc CodeFirst mode database migration steps, mvccodefirst

 

After the basic classes are constructed using the Code First mode, the project is also set up and runs successfully, and the database table structure has been automatically generated.

However, I have a new class to be added and a field to be modified. What should I do? Delete the database and run the road? Haha

 

With database migration, the original structure is not changed, and the new class is created separately, or the existing database table is changed, then the table is modified.

 

Migration steps:

1.OpenPackage Manager Console: Tools-> NuGet Package Manager-> Package Manager Console. (You can also open it in other ways. I like this)

The Package Manager Console is displayed.

Note the default project !!!

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

 

Because Models does not necessarily directly use the existing folder Models in the project, when you select the startup project as the asp.net mvc framework type, an error is reported here.

The distribution of my current project is as follows:

I have separated the model so that when I select SearchEngine as the startup project by default, the First Command entered in the Package Manager Console will report an error.

 

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

If the operation is successful, the following message is displayed:

In addition, the migrations directory is added to the DbContext directory at the same level.

 

3.Start database migration command

1. Add-migration [custom version name]

EnterDd-migrationUpdateorderheader

2. update-database

InputUpdate-database

 

If DbContext is split separately like me, you need to add the database connection address in the configuration file of the current class library.

 

If DbContext is added directly to Models in the asp.net mvc Framework, the connection string in the Web. Config file is directly read.

 

Waiting for command execution ......

An exception occurred.

 

Start searching for the SQL Server Configuration Manage in my computer. If yes, open it and find

 

 

Set the TCP/IP protocol to enabled.

This tool is not available locally. You can only find this tool as follows:

Right-click my computer and choose "manage"> "services" and "Applications"> "SQLServer Configuration Manager". (The tool is not found twice. It seems that this solution is used directly)

Re-enter the command and wait for execution

Initial success

 

Some tables in the database do not need to be changed.

We can change the specific table to be added or modified in the migrations directory.

 

 

You can modify CreateTable/DropTable to control which tables need to be modified.

namespace SAssassin.EF.Model.Migrations{    using System;    using System.Data.Entity.Migrations;    public partial class updateorderheader : DbMigration    {        public override void Up()        {            CreateTable(                "dbo.MyFileInfoes",                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()        {            DropTable("dbo.MyFileInfoes");        }    }}


4. Modifying the code configuration makes subsequent operations less troublesome.

Modify the Configuration. cs file in the migrations directory and enable automatic migration.

5. Add the following code line to Application_Start of project Global. asax:

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.

 

 , I hope I can come back and see my step when my technology is successful.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.