Create Initial Code First Migration
In order to do Code First Migrations, you first have to set up the command dnx ef. Code first means, that you first do database model changes in the Entity Framework database classes and then apply the changes to the actual database. Please read the 2 previous blog entries about ASP .NET 5 on how to set up the dnx command line tool.
- https://www.illucit.com/blog/2016/02/asp-net-5-toolchain-setup/
- https://www.illucit.com/blog/2016/02/asp-net-5-with-entity-framework-7/
To be more specific, the examples in this article were done using Entity Framework 7 RC1. If you execute dnx ef in your ASP .NET project folder, you will get the following output.
dnx ef output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
_/\__ ---==/ \\ ___ ___ |. \|\ | __|| __| | ) \\\ | _| | _| \_/ | //|\\ |___||_| / \\\/\\ Entity Framework Commands 7.0.0-rc1-16348 Usage: dnx ef [options] [command] Options: --version Show version information -?|-h|--help Show help information Commands: database Commands to manage your database dbcontext Commands to manage your DbContext types migrations Commands to manage your migrations Use "dnx ef [command] --help" for more information about a command. |
As you can see dnx ef migrations is the command to use for adding migrations to your application.
dnx ef migrations output:
1 2 3 4 5 6 7 8 9 10 11 12 |
Usage: dnx ef migrations [options] [command] Options: -?|-h|--help Show help information Commands: add Add a new migration list List the migrations remove Remove the last migration script Generate a SQL script from migrations Use "migrations [command] --help" for more information about a command. |
So if you have created a new DbContext from an existing database as described in my previous article or simply started coding a new DbContext, you can add your first migration with the command:
1 |
dnx ef migrations add "Initial Migration" |
This will generate a new folder Migrations within your project including a new class file for the initial migration.
You can add as many migrations as you like, before actually applying them to the database.
Apply Changes to the Database
After that you can apply the migration to an existing database by using the command:
1 |
dnx ef database update |
This applies all migrations you added to the database. In addition to that, it also creates a new database table __EFMigrationsHistory, which takes track of all migrations already applied to the database.
Instead of manually applying changes to the database using the command above, you can also apply all pending database changes on each application startup by executing the following command somewhere in the code e.g. in Startup.cs.
1 |
new AppContext().Database.Migrate(); |
Otherwise if you use multiple databases e.g. development, production etc., you have to apply the command once for each database.
Summary
This article demonstrates how to do Code First Migrations using the Entity Framework 7. In the next article I will show how to use the new Dependency Injection that comes with ASP .NET 5 in order to inject a DbContext into a Controller.