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)

Source: Internet
Author: User
Tags connectionstrings

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

    1. 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)

Related Article

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.