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 ...
随机推荐
- IP地址的基础划分
1.基础知识 1.1 IP地址是由网络号(net ID)与主机号(host ID)两部分组成的. 1.2 IP地址的分类 IP地址长度为32位,点分十进制(dotted decimal)地址: 采 ...
- UGUI性能优化
http://www.cnblogs.com/suoluo/p/5417152.html http://blog.csdn.net/uwa4d/article/details/54344423 htt ...
- CAS单点登录系统简介
一.cas简介 全名:Central Authentication Service特点: 1.开源的.多协议的 SSO 解决方案: Protocols : Custom Protocol . CAS ...
- LR录制https协议设置方法
1.New一个脚本,option --> port mapping --> 选择第二个 WinNet level data 2.Run-time Setting --> Prefe ...
- ECMAScript6入门-序言
本系列笔记基于阮一峰大佬的开源书籍.如果大家想看可以去该地址 本系列笔记只记录本人自己学习的过程,如果有侵权收到通知会自行下架. 如果大家看到可以直接去地址处学习,如果觉得好还望支持正版. 在此感谢阮 ...
- window内置对象学习
1.location:本页面的location对象 对象属性图示: 对象属性: 对象方法: 2.history:是本页面的浏览历史 history对象记录了用户曾经浏览过的页面(URL),并可以实现浏 ...
- 检测一个DLL文件是x64还是x86
对于一个DLL,我们如何判定其是32位的还是64位的,或者是any cpu的platform? Visual Studio提供了一个很好的工具:corflags,这个是内嵌到Developer Com ...
- 由于簇计数比预计的高,格式化操作无法完成——Allocation Unit Size Adjustments for Larger NTFS Volumes.
Allocation Unit Size Adjustments for Larger NTFS Volumes. Problem: When trying to format a new vol ...
- AWS + Stunnel + Squid ***
[需求] 第一,能***. 第二,在企业网络要能突破端口限制. [原理] 利用AWS提供的一年免费EC2服务,搭建一台自己的VPS,在VPS中利用Stunnel与本机建立加密连接,将本地http请求通 ...
- Cassandra 学习三 数据模型
Cassandra如何存储数据的概述. 集群(Cluster) ·Cassandra数据库分布在几个一起操作的机器上.最外层容器被称为集群.对于故障处理,每个节点包含一个副本,如果发生故障,副本 ...