Entityframework5.0 data migration notes-solves the problem of model change and database Reconstruction

Source: Internet
Author: User

The biggest headache after codefirst is that data changes cause database deletion and re-creation, which will lead to data loss. The seeding method described in musicstore can only meet the needs of the next test. The data migration function is introduced in entityframework5.0 to solve this problem.

Step 1:

Ef5.0 is required. It is best to use nuget for installation. Tool --> library package manager --> package Management Console (My vs2012)

 

Enter the command to install the latest EF. As follows:

Enter get-help nuget to view other commands.

Topic about_nuget Brief Description provides information about nuget Package Manager commands. This topic describes the nuget Package Manager commands in detail. Nuget is an integrated package management tool used to add libraries and tools to. Net projects. Including the following nuget cmdlets. Cmdlet description ------------------ ---------------------------------------------- get-Package gets the available package set in the package source. Install-package install the package and its dependencies in the project. Uninstall-package uninstall the package. If other packages depend on this package, the command will fail unless the-force option is specified. Update-package: update the package and its dependencies to a newer version. Add-bindingredirect checks all the assembly in the project output path and adds the BIND redirection to the application (or web) configuration file as needed. Get-project returns a reference to DTE (development tools environment) for the specified project. If no project is specified, the default project selected in the Package Manager Console is returned. Open-packagepage open the browser and point to the projecturl, licenseurl, or reportabuseurl of the specified package. Register-tabexpansion is the parameter registration tab extension of the command. Also see online documentation: http://go.microsoft.com/fwlink? Linkid = 206619 get-package install-package uninstall-package Update-package add-bindingredirect get-Project Open-packagepage register-tabexpansion

Step 2:

To allow data migration, run the following command: PM> enable-migrations to automatically generate the migration configuration file class. Add the yellow mark to the constructor to allow automatic migration. For example, you have modified the field type, length, or added a field in the model. It automatically modifies the database. But this is not over yet

namespace NoteSystem.Migrations{    using System;    using System.Data.Entity;    using System.Data.Entity.Migrations;    using System.Linq;    internal sealed class Configuration<TContext> : DbMigrationsConfiguration<TContext> where TContext:DbContext    {        public Configuration()        {            AutomaticMigrationsEnabled = true;            AutomaticMigrationDataLossAllowed = true;        }           }}

Step 3: Modify the database constructor. Here, my database is notedb. The gray part is the method we used previously. The yellow part is the support for data migration initialization.

  public NoteDb()            : base(DbNameOrDbConnectionstring)        {            Database.SetInitializer(new MigrateDatabaseToLatestVersion<NoteDb, Configuration<NoteDb>>());            //Database.SetInitializer(new DropCreateDatabaseIfModelChanges<NoteDb>());        }

Summary: This migration saves a lot of trouble, but it is not so intelligent. It is best not to manually delete the tables in the database and delete them using dbset, because all operation records of the database are recorded in the database and in the migrationhistory of the system table. Your manual record does not exist here.

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.