ASP. NET MVC 4 under Code first database migration

Source: Internet
Author: User

A. command to open

1. Open the console: view, other windows, Package Manager console;

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

  After successful creation, the Migrations directory is added.

If the following error is reported:

pm> enable-migrations
    More than one context type is 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.FirstMVCContext, use Enable-migrations-contexttypename FirstMVC.Models.FirstMVCContext.

To specify a specific data context class.

I have only one database, can not input.

3. Run the Database migration

After you start the database migration settings successfully, you need to manually create a database migration to modify the model structure.

  * * Each subsequent modification of the model interface, the following 2 commands are executed. **

The command is as follows:

(1). add-migration [Custom version name]

(2). update-database

pm> add-migration Updateorderheader

Scaffolding is being built for migrating "Updateorderheader".
The designer code for this migration file contains a snapshot of the current Code first model. This snapshot is used to calculate changes to the model the next time the migration scaffolding is built. If you make other changes to the model that you want to include in this migration, you can re-build the scaffolding by running Add-migration 201407020938316_updateorderheader again.
pm> update-database
Specify the "-verbose" tag to view the SQL statements applied to the target database.
Applying code-based migration: [201407020938316_updateorderheader].
Applying code-based migration: 201407020938316_updateorderheader.
The Seed method is running.
Pm>

4. Modify the Code configuration

When the above steps are completed,

(1). In the generated catalog file Migrations/configuration.cs

  Set automaticmigrationsenabled = false; Modified to automaticmigrationsenabled = true;

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

System.Data.Entity.Database.SetInitializer (New system.data.entity.migratedatabasetolatestversion< Firstmvc.models.firstmvccontext,migrations.configuration> ());

notes: FirstMVC.Models.FirstMVCContext The data context class to create.

migrations.configuration for successful execution enable-migrations the file class that is generated after the command.

OK, after each modified model only need to perform the above 3rd step 2 commands.

ASP. NET MVC 4 under Code first database migration

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.