Code first updates the database structure

Source: Internet
Author: User

Reference: http://blog.csdn.net/sxycxwb/article/details/12186159

0. Delete the previous database

1. Run the enable-migrations command in the package Manager Console

Go to the (PM) package Management Console (view--Other Windows--"package Management Console") Enter the following command: Enable-migrations

The hint: "No context type is found in the assembly", probably because of the "0" reason, but can be resolved by "2".

2, pm> enable-migrations -projectname <your Project name> -< Span class= "Typ" >startupprojectname <start up project Name>

< Span class= "PLN" >< Span class= "PLN" >< Span class= "PLN" >:pm> enable-migrations-projectname Mvc_models-startupprojectname MVC_WEB

< Span class= "PLN" >< Span class= "PLN" >< Span class= "PLN" > result: Checking If the context targets an existing database ...
detected database created with a database initializer. scaffolded migration ' 201407041310515_initialcreate ' corresponding to existing database. To use an automatic migration instead, delete the Migrations folder and re-run enable-migrations specifying The-enableaut Omaticmigrations parameter.
Code first migrations enabled for Project Mvc_modmvcels.

< Span class= "PLN" >< Span class= "PLN" >< Span class= "PLN" > at this point: one more folder in the project (migrations) and Configuration.cs file

3 . Open the Configuration.cs file using Visual Studio. Replace the seed method content with the following code:

context. Members.addorupdate (NewMember {m_name="Admin", M_password="Admin"                }                ); Context. Places.addorupdate (NewPlace {P_number="1"                }                ); Context. Consumrecords.addorupdate (NewConsumrecord {cr_member=NewMember {m_name="Admin", M_password="Admin"}, Cr_place=NewPlace {P_number="1"                    }                }                );
View Code

The code-First migration mechanism calls the seed method after each migration, and if there is row data, the method updates the existing data and if it does not, the method inserts the data.

Press Ctrl+shift+b to build the project ( If this build operation is not performed here, subsequent steps will fail )
4 . The next step is to create the Dbmigration class to initialize the migration. The migration creates a new database, which is why you deleted the database files in the previous steps.

pm> add-migration-projectname mvc_modmvcels firstmigration

Scaffolding is being built for migrating "firstmigration". 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 201310140747227_firstmigration again.

Pm>

Code-First migration mechanism creates another class file under the Migrations folder, with the file name Timestamp + underscore + FirstMigration.cs, for example 201310140747227_ FirstMigration.cs, which contains the code that creates the database schema. Migration file names are preset as timestamps to help sort. View the file, which contains a description of the Create Movie library table. When you update the database, the class is executed and the database schema is created. The seed method is then executed and the test data is added.

< Span style= "FONT-SIZE:14.44PX;" > In the Package Manager Console window, type the "update-database" command to create the database and execute the seed method.

pm> update-database-projectname Xwb.Core.Data

Applying code-based migrations : [firstmigration].

Applying code-based migrations :  firstmigration.

running Seed method.

pm>

6, automatic migration

Make the following modifications in the generated configuration:

Public Configuration () {automaticmigrationsenabled = true;         Automaticmigrationdatalossallowed = true; }

①automaticmigrationsenabled: Gets or sets a value that indicates whether automatic migration can be used when migrating the database.

②automaticmigrationdatalossallowed: Gets or sets a value that indicates whether data loss during automatic migration is acceptable. If set to False, an exception is thrown when data loss may occur as part of an automatic migration.

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.