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 ...
随机推荐
- 3.16 draw 3.17 更新函数
3.16 draw virtual void draw(); void HelloWorld::draw() { CCSize size = CCDirector::sharedDirector()- ...
- 【转】Python获取当前系统时间
转自:https://www.cnblogs.com/-ldzwzj-1991/p/5889629.html 取得时间相关的信息的话,要用到python time模块,python time模块里面有 ...
- .net remoting和wcf自托管——一个bug引发的警示
一.解决问题,需要深入,并从细节入手,多从代码找原因,不能认为代码是死的,不会出错: 之前代码都运行良好,突然某一天,在我电脑上出问题了.出了问题,那就应该找出原因.其实这个问题,本身并不难,好歹给你 ...
- Kerberos的hive链接问题
javax.security.auth.login.LoginException: Checksum failed 之前碰到过类似的问题,都是因为服务器端的keytab问题:多半是因为重新生成了key ...
- JSON格式化以及转化为Entity事项
其实JSON在eclipse里面就可以自动化,不需要再联网去进行格式化: 然后通过http://www.bejson.com/java2pojo/ 即可实现json转java 但是要注意:从str ...
- 浅谈Manacher
\(Manacher\)是由一个叫做\(Manacher\)的人发明的能在\(O(n)\)时间内找出一个字符串长度最长的回文子串的算法. 由于偶回文串形如\(abba\)这样的不好找对称中心,所以我们 ...
- CentOS7 yum安装mysql5.5/5.6并初始化
https://blog.csdn.net/petrel2015/article/details/78822466 下载MySQL yum仓库文件 首先根据官网给出的建议,下载MySQL的仓库文件 h ...
- Jet Brains家族XX方法
声明:本文转载自 https://www.jianshu.com/p/f404994e2843 2018/11/23
- Kosaraju
https://www.cnblogs.com/nullzx/p/6437926.html
- laravel 添加自定义类 全局自定义方法 自定义常量
添加自定义类 https://blog.csdn.net/suchfool/article/details/38758367 https://blog.csdn.net/liukai6/article ...