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
[CSharp]View PlainCopy
- Original model
[CSharp]View PlainCopy
- Using System.Collections;
- Using System.Collections.Generic;
- Using System.ComponentModel.DataAnnotations;
- Public class Lesson {
- public int Lessonid { get; set;}
- [Required]
- [MaxLength (50)]
- public string Lessonname { get; set;}
- [Required]
- public string TeacherName { get; set;}
- public Virtual UserInfo userinfo{get; Set;}
- }
[CSharp]View PlainCopy
- New model
[CSharp]View PlainCopy
- Using System.Collections;
- Using System.Collections.Generic;
- Using System.ComponentModel.DataAnnotations;
- Public class Lesson {
- public int Lessonid { get; set;}
- [Required]
- [MaxLength (50)]
- public string Lessonname { get; set;}
- [Required]
- [MaxLength (10)]
- public string TeacherName { get; set;}
- public Virtual UserInfo userinfo{get; Set;}
- }
Note: The difference is that we add a length limit to the TeacherName attribute.
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:
[HTML]View PlainCopy
- <connectionStrings>
- <add name= "testusersdb" connectionstring= "Integrated Security=sspi; Persist Security info=false;initial catalog=testusersdb;data source=xcl-pc\sqlexpress " providername=" System.Data.SqlClient " />
- </connectionStrings>
2: Open the NuGet console:
3: Run command enable-migrations
The following error may occur:
Checking If the context targets an existing database ...
Detected database created with a database initializer. scaffolded migration ' 201212090821166_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 MvcApplication1.
The following folder appears in the project:
Opening the Configuation.cs will make the following changes:
[CSharp]View PlainCopy
- Public Configuration ()
- {
- automaticmigrationsenabled = true;
- }
Execute Update-database again:
Since I changed the length from Max to 10, when I update the data structure, it thinks this operation will result in data loss, as follows:
Specify the '-verbose ' flag to view the SQL statements being applied to the target database.
No pending code-based migrations.
Applying automatic migration:201212090848057_automaticmigration.
Automatic migration was wasn't applied because it would result in data loss.
If you're sure it's okay, just add a mandatory parameter to the command:
Enable-migrations-force
Last re-execution: update-database
The original data in the database is not lost!
3:
Code first migrations Updating the database structure (data migration)