Asp. NET no magic--asp.net MVC and database entity Framework migrations

Source: Internet
Author: User

When developing a database application, it is often encountered that some tables need to add fields or modify types, new tables, and so on, whereas for EF Code first there are only entity classes that need to be added to a new entity class or added, removed, or deleted in the entity class when requirements change. You can modify the properties. But how do I synchronize the changes to the database after the modifications are complete?

The Entity Framework provides a migrations mechanism to address this problem.

Enable migrations

In the Package Manager console window in VS, select the project that has the default project as DbContext and execute the command:
  Enable-migrations

  

The Migrations directory and the configuration type are then generated in the project:

  

  

Construction method: Used for some migrations-related configuration, such as the above code is disabled automatic migration.
Seed method: Based on annotations, it is common to add default data after migration. The configuration type can be modified as required.

Note: If the following error occurs, the namespace can be removed for repair:

add migration via Add-migration

If a database already exists then you need to use the –ignorechanges parameter to create an empty migration to correspond to the current database model:
  Add-migration initialcreate–ignorechanges

  

A time-stamped InitialCreate.cs file is generated in the Migrations directory:

  

Note: The Up and down methods of the above code are empty because the –ignorechanges parameter is used because the corresponding tables and fields are already in the database, so it should not be created again, and if you remove the parameter, the following code will be generated:

  

update-database Updating the database

Execute the Update-database directory to update the database:
  update-database-startupprojectname ' My Blog '-projectname blogrepository.mysql
  Note: Because the DbContext correlation type in this example is in Blogrepository.mysql, the connection string is in the My blog project, so you need to specify two parameters for startup project and project name when you perform a database update. In general, you can use the Update-database command.

  

At this point there is more than one name in the database called the __migrationshistory table and there is a record.

  

Update the model and add a new migration

1. Add a new attribute "Ispublish" to the model to indicate whether the article is exposed:

   

2. Run the add-migration command, preferably with a change-related name to differentiate multiple migration for ease of maintenance:
  Add-migration Addispublishcolumn

  

After executing the command, a subclass of Dbmigration is generated that contains an up and down method that corresponds to the operation of the database when updating and fallback:

  

The up method above is to add a column named "Ispublish" for the dbo.posts table and delete the column if the migration is rolled back.

3. Use the Update-database command again to synchronize the new changes to the database:

  Update-database-startupprojectname ' My Blog '-projectname blogrepository.mysql-verbose

  

Note: Use the parameter-verbose to see more execution information, including executed SQL statements.

automatically update the database to the latest when you start the app

Update the database to the latest when launching the app you can omit the "update-database" command to perform this procedure, only add migration, and the migration is not synchronized to the database, then the application executes ( The DbContext is actually onmodelcreating when the model is created) and the changes are synchronized to the database.

1. Add the Clickcount property:

  

2. Add the Onmodelcreating method and code to the DbContext type (note: The operation can also be configured through the configuration file):

  

3. Run the program:

  

4. View the results:

  

about automatic migrations (automatic migration)

Automatic migration refers to the comparison of the entity model with the database structure when executing the "update-database" command, and, if they differ, the entity model , which synchronizes the differences to the database. The equivalent of omitting the add-migration command.

When you execute the start Migration command (enable-migrations), you can add parameters – Enableautomaticmigrations or set the value of automaticmigrationsenabled to true in the construction method that generates the configuration type.

  

Automatically updates the database (migrate to latest version) with automatic migration (automatic migrations), which omits "Update-database", which omits "add-migration", If used together, both are omitted, developers only need to open the migration through the enable-migrations command, and then modify the completion of the entity class in development, you can debug the program, the debugger process automatically update the database structure.

  

Summary:
This chapter focuses on the migration capabilities of EF, which is a very important feature for EF, because in the development process, especially during the code first development process, it is common to add entities, modify entity properties, and even use the fluent API to map database relationships will also be modified for some properties, such as field type, maximum length, index, and so on.

For automatic database updates and automatic migration, it is not recommended for individuals to be used when a team is developing a formal project because the add-migration command is a good place to track updates to the database structure and can be rolled back as appropriate. Updating the database automatically results in a change in the database structure, and if multiple people use the same database in the team's work and cause errors when the code is not synchronized.
But it saves a lot of work to enable automatic migration and to automatically update the database to the latest when setting up database initialization. Each has advantages and disadvantages can be selected according to circumstances.

Reference:

https://msdn.microsoft.com/en-us/library/jj591621 (v=vs.113). aspx

This article connects: http://www.cnblogs.com/selimsong/p/7656648.html

Asp. NET no magic--Directory

Asp. NET no magic--asp.net MVC and database entity Framework migrations

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.