zoukankan      html  css  js  c++  java
  • Entity Framework Code-First(22):Code-based Migration

    Code-based Migration:

    Code-based migration is useful when you want more control on the migration, i.e. set default value of the column, etc.

    Code-First has two commands for code based migration:

    1. Add-migration: It will scaffold the next migration for the changes you have made to your domain classes
    2. Update-database: It will apply pending changes to the database based on latest scaffolding code file you created using "Add-Migration" command

    Assume that you have Student and Course entity classes initially and you want to use code-based migration for your application. Before running the commands above, you must enable migration for your application, by using the enable-migrations commands. These are in package manager that we used previously for automatic migration. This will create a configuration file, as was the case with automated migration. Also, you need to set the database initializer in the context class:

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

    Now, you have to create a scaffold code file which consists of your database requirement from your existing domain classes. You can do this by running the “add-migration" command in the package manager. (from Tools → Library Package Manager → Package Manager Console). You will have to pass the name parameter, which will be part of the code file name.

    automated migration in code first

    Add-Migration command Syntax:

        
        Add-Migration [-Name] <String> [-Force]
          [-ProjectName <String>] [-StartUpProjectName <String>]
          [-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
          [-IgnoreChanges] [<CommonParameters>]
     
        Add-Migration [-Name] <String> [-Force]
          [-ProjectName <String>] [-StartUpProjectName <String>]
          [-ConfigurationTypeName <String>] -ConnectionString <String>
          -ConnectionProviderName <String> [-IgnoreChanges] [<Common Parameters>]
            

    You can see that this command has created a new file in the Migration folder with the name of the parameter you passed to the command with a timestamp prefix:

    code based migration in code first

    After creating the file above using the add-migration command, you have to update the database. You can create or update the database using the “update-database” command. You can use –verbose to see what's going on in the database:

    code based migration in code first

    Update-Database command syntax:

    Update-Database [-SourceMigration <String>]
        [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>]
        [-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
        [-ConnectionStringName <String>] [<CommonParameters>]
     
    Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
        [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
        [-ConfigurationTypeName <String>] -ConnectionString <String>
        -ConnectionProviderName <String> [<CommonParameters>]
            

    At this point, the database will be created or updated.

    Now, suppose you added more domain classes. So before running the application, you have to create a scaffold file for new classes, by executing the "Add-Migration" command. Once it creates the file, update the database using the Update-Database command. In this way, you have to repeat the Add-Migration and Update-Database command each time you make any changes in your domain classes.

    Rollback Database change:

    Suppose you want to roll back the database schema to any of the previous states, then you can use "update-database" command with –TargetMigration parameter as shown below:

    update-database -TargetMigration:"First School DB schema"

    Use the "get-migration" command to see what migration has been applied.

    Note: Use the "get-help" command for add-migration and update-database command in order to see what parameters can be passed with this command.

  • 相关阅读:
    安信天行全方位信息安全态势感知平台建设与运营
    SQL基础总结——20150730
    中兴推“小兴看看”,诠释智能家电的真谛
    Java 线程第三版 第九章 Thread调度 读书笔记
    3930: [CQOI2015]选数|递推|数论
    S​D​I​与​A​S​I 接口具体解释介绍
    通过双重for循环来找到JSON中不反复的数据
    蓝桥杯 2016/3/17 測试 前6题题解...
    [疯狂Java]JDBC:事务管理、中间点、批量更新
    Linux下搭建Memcached缓存系统
  • 原文地址:https://www.cnblogs.com/purplefox2008/p/5644383.html
Copyright © 2011-2022 走看看