EF CodeFirst Mode Automatic migration (applicable to the development environment), efcodefirst

Source: Internet
Author: User

EF CodeFirst Mode Automatic migration (applicable to the development environment), efcodefirst
Automatic migration in CodeFirst mode of EF (applicable to the development environment)

1. Enable EF data migration

NuGet Package Manager ------> package management console --------> Enable-Migrations

2. Set the database context to migrate to the last version.

MigrateDatabaseToLatestVersion <database context, migration Configuration File>

 

Using Models. migrations; namespace Models {public class AppDBContext: DbContext, IDisposable {static AppDBContext () {Database. setInitializer <AppDBContext> (new MigrateDatabaseToLatestVersion <AppDBContext, Configuration> ();} public AppDBContext (): base ("DefaultConnection") {Database. log = GetLog; // obtain the SQL executed by EF} // <summary> // release the resource /// </summary> public new void Dispose () {base. dispose (); GC. SuppressFinalize (this);} // <summary> // destructor /// </summary> ~ AppDBContext () {base. dispose ();} private void GetLog (string SQL) {// log output to the console System. diagnostics. debug. write (SQL);} protected override void OnModelCreating (DbModelBuilder modelBuilder) {// solve the problem that the name of the database table in the EF dynamic database is changed to the plural modelBuilder. conventions. remove <PluralizingTableNameConvention> ();}}

3. Configure the migration configuration file to allow automatic migration andData loss is allowed during migration (only applicable to the development environment)

namespace Models.Migrations{    using System;    using System.Data.Entity;    using System.Data.Entity.Migrations;    using System.Linq;    internal sealed class Configuration : DbMigrationsConfiguration<Models.AppDBContext>    {        public Configuration()        {            AutomaticMigrationsEnabled = true;            AutomaticMigrationDataLossAllowed = true;            ContextKey = "Models.AppDBContext";        }        protected override void Seed(Models.AppDBContext context)        {            //  This method will be called after migrating to the latest version.            //  You can use the DbSet<T>.AddOrUpdate() helper extension method             //  to avoid creating duplicate seed data.        }    }}

Entity changes. Manual migration is no longer required. The database will be automatically updated. Setting AutomaticMigrationDataLossAllowed to true for migration may cause data loss.

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.