zoukankan      html  css  js  c++  java
  • 【译】第35节---自动迁移

    原文:http://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx

    EF4.3引入了自动迁移,因此你不必在代码文件中手动处理数据库迁移。对于你在域类中进行的每次更改, 只需要在Package Manger Console中运行命令即可完成此操作。

    让我们看看如何使用自动迁移。

    当你开始编写Code-First应用程序时,你没有数据库。 例如,我们开始使用学生和课程实体类编写应用程序。 在运行尚未创建其数据库的应用程序之前,必须通过在Package Manager Console中运行“enable-migrations”命令来启用自动迁移,如下所示:

    首先,从Tools→Library Package Manager→Package Manager Console打开软件包管理器控制台,然后运行“enable-migrations -EnableAutomaticMigration:$ true”命令(确保默认项目是您的上下文类的项目)

    一旦命令成功运行,它将在你的项目的Migration文件夹中创建一个internal sealed的配置类:

    如果你打开它并看到下面显示的类,那么你将在构造函数中找到AutomaticMigrationsEnabled = true:

     internal sealed class Configuration : DbMigrationsConfiguration<SchoolDataLayer.SchoolDBContext>
        {
            public Configuration()
            {
                AutomaticMigrationsEnabled = true;
            }
    
            protected override void Seed(SchoolDataLayer.SchoolDBContext 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" }
                //    );
                //
            }
        }

    你还需要在上下文类中使用新的数据库初始化策略MigrateDatabaseToLatestVersion设置数据库初始化程序,如下所示:

    public class SchoolDBContext: DbContext 
    {
        public SchoolDBContext(): base("SchoolDBConnectionString") 
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext, SchoolDataLayer.Migrations.Configuration>("SchoolDBConnectionString"));
                
        }
    
        public DbSet<Student> Students { get; set; }
        public DbSet<Course> Courses { get; set; }
            
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
    
            base.OnModelCreating(modelBuilder);
        }
    
    }

    如你在上面显示的代码中可以看到的,我们已经通过了由命令创建的Configuration类名以及上下文类名。

    现在,你已设置为自动迁移。 当你更改模型时,它会自动处理迁移。 运行应用程序并查看创建的数据库:

    你将发现它已经创建了一个系统表__MigrationHistory以及其他表。 这是自动化迁移维护数据库更改历史的地方。

    现在我们来添加一个Standard实体类。 再次运行应用程序,将看到它已经自动创建了一个Standard表。

    等一下,如果你添加一个新的实体类或者删除一个实体类,但是从实体类添加或删除属性怎么办? 为了尝试,我们从Standard类中删除Description属性并运行应用程序。

    哎呀...将显示一条错误消息:

    这是因为如果将其从Standard类中删除,则会丢失Description列中的数据。

    所以要处理这种情况,必须在配置类构造函数中设置AutomaticMigrationDataLossAllowed = true,以及AutomaticMigrationsEnabled = true。

    注意:可以使用“get-help enable-migrations”命令查找有关可以传递给enable-migrations命令的参数的更多信息。 有关更多详细帮助,请使用“get-help enable-migrations -detailed”

    因此,迁移可以自动处理。

  • 相关阅读:
    SLS评测报告
    Flash对不同的浏览器的兼容性
    NodeJS的Cluster模块使用
    Varnish+Xcache构建高性能WEB构架初探
    Memcached Client的释疑
    Firebug及YSlow简介与使用图文详解
    PHP Memcached 实现简单数据库缓存
    PHP + Memcache 实现Session共享
    Linux 开机关机在线求助与指令输入
    Linux 基础学习篇笔记 Linux基础知识
  • 原文地址:https://www.cnblogs.com/talentzemin/p/7323857.html
Copyright © 2011-2022 走看看