Code First database migration under ASP. net mvc 4, asp. netmvc

Source: Internet
Author: User

Code First database migration under ASP. net mvc 4, asp. netmvc

1. Enable the command

1. Open the console: View> other Windows> Package Manager Console;

 

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

  After the creation is successful, the migrations directory will be added.

If the following error is reported:

 

PM>Enable-Migrations
More than one context type was found in the assembly 'firstmvc '.
To enable migrations for FirstMVC. Models. UsersContext, use Enable-Migrations-ContextTypeName FirstMVC. Models. UsersContext.
To enable migrations for FirstMVC. Models. fir1_vccontext, use Enable-Migrations-ContextTypeName FirstMVC. Models. fir1_vccontext.

Specify a data context class.

I only have one database and do not need to enter it.

 

3. Run database migration

After the database migration settings are successfully started, you must manually create a database migration task to modify the Model structure.

** Execute the following two commands each time you modify the Model interface. **

The command is as follows:

(1). Add-migration [custom version name]

(2). update-database

PM>Add-migration updateorderheader

Constructing a base frame for migrating "updateorderheader.
The designer Code of this migration file contains a snapshot of the current Code First model. This snapshot will be used to calculate the changes to the Model during the next build of the migration base. If you want to make other changes to the model that you want to include in this Migration, you can re-run "Add-Migration 2014070209381__updateorderheader" to re-build the infrastructure.
PM>Update-database
Specify the "-Verbose" flag to view the SQL statements applied to the target database.
Application code-based migration: [2014070209381__updateorderheader].
Application code-based migration: 2014070209381__updateorderheader.
Running the Seed method.
PM>

 

4. modify the code configuration

After the preceding steps are completed,

(1). In the generated directory file, Migrations/Configuration. cs

Set AutomaticMigrationsEnabled = false; To AutomaticMigrationsEnabled = true;

(2) Add the following line of code in Application_Start of project Global. asax:

System. Data. Entity. Database. SetInitializer (new System. Data. Entity. MigrateDatabaseToLatestVersion <FirstMVC. Models. fir1_vccontext, Migrations. Configuration> ());

Note:FirstMVC. Models. fir1_vccontext is the created data context class.

Migrations. Configuration is successfully executedEnable-MigrationsFile class generated after the command.

 

OK. After modifying the Model, you only need to execute the two commands in step 1.

 

 


For MVC4, how many models in codefirst are mapped to the same database?

Put it in the same project. Opening up does not make much sense
 
All aspnet mvc3 projects are developed using codefirst, right?

Your answer is basically correct, but there is always an accident. I am currently working on the MVC3 project, so this is not the case.

In fact, you are a method of development. Your favorite and suitable projects are the best. Of course, the premise is to meet the needs of the project, rather than simply relying on your own interests.

Hey. It's off duty ,. Eat now.

Related Article

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.