using MvcShopping.Migrations;
using MvcShopping.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace MvcShopping
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            //Global.asax.cs配置自动数据迁移
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<MvcShoppingContext, Configuration>());

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
}

namespace MvcShopping.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;

    /// <summary>
    /// ~/Migrations/Configuration.cs
    /// </summary>
    internal sealed class Configuration : DbMigrationsConfiguration<MvcShopping.Models.MvcShoppingContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = true; //开启 ①修改数据模型 ②项目重新生成 ③F5调试 ④查看数据库
            AutomaticMigrationDataLossAllowed = true;
        }

        protected override void Seed(MvcShopping.Models.MvcShoppingContext 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. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }
    }
}


本文转载:CSDN博客