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:
- Add-migration: It will scaffold the next migration for the changes you have made to your domain classes
- 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.
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:
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:
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.
最新文章
- eclipse远程调试
- 如何重置mysql的密码
- 慎用StringEscapeUtils.escapeHtml步骤
- iOS - Responder Chain
- 学习日记_SSH框架web.xml配置文件篇
- 我的Python成长之路---第四天---Python基础(15)---2016年1月23日(寒风刺骨)
- 笔记:Ubuntu 上的Testlink 部署
- PS小实验-去除水印
- 程序员的自我救赎---1.4.2: 核心框架讲解(BLL&;Tool)
- WebApi 参数绑定方法
- 命令行窗口中用telnet测试HTTP协议
- 面试时怎样回答:你对原生ajax的理解
- Caused by: java.lang.ClassNotFoundException: org.springframework.web.filter.FormContentFilter
- Hadoop生态组件的WebUI地址
- Spring使用笔记(一)Spring简介
- Python自动化编程-树莓派GPIO编程(二)
- MySql事务select for update及数据的一致性处理讲解
- Go-MySQL-Driver
- vue-10-混合
- 初期测评 E 迷障
热门文章
- linux查看网络链接状况命令netstat
- IntellJ IDEA快捷键汇总
- gulp之sass 监听文件,自动编译
- resize2fs: Bad magic number in super-block while trying to open /dev/centos/root Couldn&#39;t find valid filesystem superblock
- composer的安装和使用
- Java基础--单例类创建和测试
- DotNetBar笔记
- Hudson和Jenkins的关系
- strcmp与strncmp的区别
- leetcode Single Number II - 位运算处理数组中的数