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

  • 相关阅读:
    c++ primer 中讲的顶层const 和 底层 const 理解
    github 0 学习
    MySQL 0 学习
    c++11 move构造函数和move operator 函数 学习
    c++11 多线程 1
    c++ 多线程 0
    学习 emplace_back() 和 push_back 的区别 emplace_back效率高
    crontab执行脚本失败问题
    lucene 排序
    maven 内置变量
  • 原文地址:https://www.cnblogs.com/smartsmile/p/6234102.html
Copyright © 2011-2022 走看看