zoukankan      html  css  js  c++  java
  • Entity Framework 之Code First自动数据迁移

    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" }
                //    );
                //
            }
        }
    }
    

  • 相关阅读:
    java实现排列序数
    java实现猜算式
    java实现猜算式
    java实现猜算式
    java实现猜算式
    java实现猜算式
    java实现算年龄
    Delphi 项目失败的总结
    使用EurekaLog时遇到的问题
    KmdKit4D 0.01正式版发布了(0.02版已放出)(Delphi做驱动)
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234102.html
Copyright © 2011-2022 走看看