Background
Code first when the model is modified, to persist in the database, the original database is always deleted and then created (dropcreatedatabaseifmodelchanges), this time there is a problem, when our old database contains some test data , we can introduce the data migration function of EF when the original data is lost after the persistent update.
Requirements
- NuGet is installed
Procedure Examples Original Model
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Data.Entity;namespacemvcapplication1.models{ Public classMovie { Public intID {Get;Set; } Public stringTitle {Get;Set; } PublicDateTime ReleaseDate {Get;Set; } Public stringGenre {Get;Set; } Public decimalPrice {Get;Set; } Public stringRating {Get;Set; } } Public classMoviedbcontext:dbcontext { PublicDbset<movie> Movies {Get;Set; } }}
//New Model
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Data.Entity;namespacemvcapplication1.models{ Public classMovie { Public intID {Get;Set; } Public stringTitle {Get;Set; } PublicDateTime ReleaseDate {Get;Set; } Public stringGenre {Get;Set; } Public decimalPrice {Get;Set; } Public decimalVipprice {Get;Set; } Public stringRating {Get;Set; } } Public classMoviedbcontext:dbcontext { PublicDbset<movie> Movies {Get;Set; } }}
Note: The difference is that we added a new Vipprice attribute to movie.
Next, we're going to start persisting this model into the database (we're just modifying the property now, the length of this field in the database is nvarchar (max), not nvarchar (10))
1: Configure the database connection in config:
< connectionStrings > < name= "Moviedbcontext" connectionString= "Data source= (LocalDB) \v11.0; attachdbfilename=| datadirectory|\movies.mdf;integrated security=true " providerName=" System.Data.SqlClient " /> </connectionStrings>
2: Open the NuGet console:
3: Run command enable-migrations
The following error may occur:
pm> enable-migrationsmore than one context type was found in The assembly " mvcapplication1 " for MvcApplication1.Models.UsersContext, use Enable-migrations-contexttypename MvcApplication1.Models.UsersContext.To Enable Migrations for MvcApplication1.Models.MovieDBContext, use Enable-migrations-contexttypename MvcApplication1.Models.MovieDBContext.To Enable Migrations for mvcapplication1.purchaserequestentities, use Enable-migrations-contexttypename mvcapplication1.purchaserequestentities.
This is because I have performed enable-migrations before and the system prompts me to already exist MvcApplication1 context
The following folder appears in the project:
Opening the Configuation.cs will make the following changes:
Public Configuration () { automaticmigrationsenabled = true; }
4: Execute update-databaseagain:
As follows:
Pm> update-DatabaseSpecifies the "-Verbose" tag to view the SQL statements applied to the target database. There are no pending code-based migrations. Applying automatic migration: 201501220847062_automaticmigration. The Seed method is running.
If you're sure it's okay, just add a mandatory parameter to the command:
Enable-migrations-force
Last re-execution: update-database
The Vipprice field has been added to the database
The original data in the database is not lost!
One of the special articles for getting Started with ASP MVC4----Code First migrations updating the database structure (data Migration) modifying the entity FrameWork data structures (without deleting data)