Code Migrations Update database structure (data migration)

Source: Internet
Author: User
Tags connectionstrings
Background

Code first when the model was modified to be persisted into the database, the original database was deleted and then created (Dropcreatedatabaseifmodelchanges), which would create a problem when our old database contains some test data , when the update is persisted, the original data will all be lost, so we can introduce the EF data migration function to complete.


Requirements

NuGet already installed

Procedure Example

Original model
Using System.Collections;
Using System.Collections.Generic;
Using System.ComponentModel.DataAnnotations;
public class Lesson {public
    int Lessonid {get; set;}
    [Required]
    [MaxLength (m)]
    public string Lessonname {get; set;}
    [Required]
    public string TeacherName {get; set;}
    public virtual UserInfo Userinfo{get;set;}
New model
Using System.Collections;
Using System.Collections.Generic;
Using System.ComponentModel.DataAnnotations;
public class Lesson {public
    int Lessonid {get; set;}
    [Required]
    [MaxLength (m)]
    public string Lessonname {get; set;}
    [Required]
    [MaxLength (Ten)]
    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'll start to persist this model to 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>
    <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 a 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 Omaticmigrations parameter.
The Code-migrations enabled for Project MvcApplication1.

The following folder appears for the project:



Opening the Configuation.cs will make the following modifications:

        Public Configuration ()
        {
            automaticmigrationsenabled = true;
        }

Perform update-database again:

Because I changed the length from Max to 10, when I updated the data structure, it assumed that this would cause data loss, as follows:

Specify the "-verbose ' flag to view" SQL statements being 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 executed again: update-database



The original data in the database is also not lost.


3:


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.