After you build the database by using code first
When you run a program when a database changes, a problem with data changes can be removed by removing the database rebuild resolution
But the previous data cannot be retained in order to preserve the previous database data we need to use the Code First Data migration
First, if you need to change the data structure, you must first change the model class and then use the Code Firs Data Migration If you change the database directly before using data migration will cause an error.
Using the Code Firs Data migration plot
Tools--"library package Program Manager--" Package management Console
Observe the default project and package source
Because only the EF framework is installed to use it, it is only possible to put DBContext.cs in the default startup directory.
However, we do not do this when we are developing the project. If you simply cite EF and don't get the effect of installing EF, you can use the following method
When putting DBContext.cs in other directories, here's the model case.
The default selection as model directly executes the following command will error this is because this directory is missing EF5.0 we need to install EF5.0
execute this command install-package entityframework-version 5.0.0 Install EFAt the time of the 5.0 version of EF installed after the model class, such as after the installation of the model class will appear packages.config now we can start the normal operation of data migration first execute the commandenable-migrations-contexttypename moviedbcontext Open Code First migration will appear as After opening the program will be added by default migrations This folder opens Configuration.cs
Public Configuration () { false;}
Set automaticmigrationsenabled = false; Change to automaticmigrationsenabled = true; Turn on automatic Code first migration
And then there are two ways to do the first approach
Execute the command add-migration Initial to build the scaffolding for the migration "Initial". After execution as
The code-First migration mechanism creates another class file under the Migrations folder, with the file name Timestamp + underscore + Initial.cs, and the class is executed to create the database schema. The seed method is then executed and the test data is added.
Then execute the command update-database to complete the data migration ...
The second method of
Execute the command add-migration addratingmig to build the scaffolding for the migration "Addratingmig". After execution as
The code-First migration mechanism creates another class file under the Migrations folder, with the file name Timestamp + underscore + AddRatingMig.cs, which will be executed, automatically compare against the database, and create the database schema. The seed method is then executed and the test data is added.
Then execute the command update-database to complete the data migration ...
This makes it possible to modify the model class without deleting the database rebuild using code first to migrate the data without worrying about the previous data being deleted!
MVC4 Code First Data Migration solution