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.
Entity Framework Code-First(22):Code-based Migration的更多相关文章
- Entity Framework Tutorial Basics(22):Disconnected Entities
Disconnected Entities: Before we see how to perform CRUD operation on disconnected entity graph, let ...
- Entity Framework Tutorial Basics(11):Code First
Code First development with Entity Framework: Entity Framework supports three different development ...
- Entity Framework Tutorial Basics(37):Lazy Loading
Lazy Loading: One of the important functions of Entity Framework is lazy loading. Lazy loading means ...
- Entity Framework Tutorial Basics(36):Eager Loading
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...
- Entity Framework Tutorial Basics(32):Enum Support
Enum in Entity Framework: You can now have an Enum in Entity Framework 5.0 onwards. EF 5 should targ ...
- Entity Framework Tutorial Basics(29):Stored Procedure in Entity Framework
Stored Procedure in Entity Framework: Entity Framework has the ability to automatically build native ...
- Entity Framework Tutorial Basics(28):Concurrency
Concurrency in Entity Framework: Entity Framework supports Optimistic Concurrency by default. In the ...
- Entity Framework Tutorial Basics(27):Update Entity Graph
Update Entity Graph using DbContext: Updating an entity graph in disconnected scenario is a complex ...
- Entity Framework Tutorial Basics(19):Change Tracking
Change Tracking in Entity Framework: Here, you will learn how entity framework tracks changes on ent ...
- Entity Framework Tutorial Basics(15):Querying with EDM
Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...
随机推荐
- 1150 Travelling Salesman Problem(25 分)
The "travelling salesman problem" asks the following question: "Given a list of citie ...
- Python函数-enumerate()
enumerate(sequence, [start=0]) 作用: 将可循环序列sequence以start开始分别列出序列数据和数据下标,即对一个可遍历的数据对象(如列表.元组或字符串),enum ...
- java代码I/O类
总结:流类无法理解啊—————— package com.aini; import java.io.*; //流类 //使用FileInputStream读取文件信息 public class ffg ...
- appium python版api
打印上下文 driver.contexts 打印当前上下文 driver.context driver.current_context 切换上下文 driver.switch_to.context(' ...
- 2015.1.15 利用航线id取所有点的函数创建视图
1.根据航路id取所有航路点的函数 create or replace function alinepnts(alid in number) return tab_airline_pnt is --返 ...
- c++如何编写线程安全的DLL
DLL有个共同的特点就是都有一个初始化函数,一个资源释放函数,其他几个函数都是核心功能函数.而且这些DLL有时会被多个进程同时调用,这就牵扯到多进程的多线程调用DLL的问题.有点绕口,以下我根据我实践 ...
- 系统监控磁盘分区 homework
作业一: 1) 开启Linux系统前添加一块大小为15G的SCSI硬盘 2) 开启系统,右击桌面,打开终端 3) 为新加的硬盘分区,一个主分区大小为5G,剩余空间给扩展分区,在扩展分区上划分1个逻辑分 ...
- 11-16网页基础--HTML
网页制作部分主要讲解三大部分: 1.HTML 超文本标记语言( 全称:Hyper Text Markup Language) 专门编辑静态网页 2.CSS 网页美化:是HTML控制的 ...
- Struts2+Hibernate+Spring 整合示例
转自:https://blog.csdn.net/tkd03072010/article/details/7468769 Struts2+Hibernate+Spring 整合示例 Spring整合S ...
- elasticsearch的功能及适用场景(2)
1.Elasticsearch的功能 (1)分布式的搜索引擎和数据分析引擎 搜索:百度,网站的站内搜索,IT系统的检索数据分析:电商网站,最近7天牙膏这种商品销量排名前10的商家有哪些:新闻网站,最近 ...