The following post describes the steps to setup and use Entity Framework Code First Migrations. For more information read this Microsoft page. The sample application shown in this article can be found on GitHub.
Initial setup
Listed below are the steps to setup a new project with an Entity Framework context, an entity class and an initial migration:
- Create a new data access project (C# library project) in your solution
- Open the Package Manager Console in the Visual Studio menu Tools > NuGet Package Manager > Package Manager Console. In the console window, select the previously created data access project as default project.
-
Install Entity Framework as NuGet package:
Install-Package EntityFramework
-
Implement a data context class which inherits from
DbContext
and the entity classes:public class DataContext : DbContext { public DbSet<Person> Persons { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { ... } } public class Person { ... }
-
Enable migrations in the Package Manager Console:
Enable-Migrations
-
Create an initial migration:
Add-Migration InitialCreate
Note: “InitialCreate” is the default name for the first migration.
-
Update the database with any pending migrations:
Update-Database -Verbose
If no database can be found, the command creates a new database in a LocalDB (a file-based Microsoft SQL instance). By default, you can connect to this database by using the connection string
(localdb)\mssqllocaldb
in the Microsoft SQL Server Management Studio. The instance name may be altered by changing theparameter
value in the configuration file (App.config
orWeb.config
):<entityFramework> <defaultConnectionFactory type="...LocalDbConnectionFactory, ..."> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> ... </entityFramework>
- The command
Update-Database
created anApp.config
file in your data access project. If this project is not your startup project, you need to copy the Entity Framework configuration from theApp.config
file to the application configuration file (e.g. theWeb.config
file of your web project).
Create a new migration
To create a new migration after changing the entity model, just rerun the following command with an appropriate migration name:
Add-Migration MyMigration
Regenerate the database
To delete the existing database and completely regenerate the database and its tables, follow the following steps:
- Open Microsoft SQL Server Management Studio
- Connect to your SQL Server or LocalDB with the Microsoft SQL Server Management Studio
- Delete the database
-
Optional: If you don’t need to migrate other databases, delete all migration classes from the “Migrations” directory of the data access project and recreate the first migration:
Add-Migration InitialCreate
-
Regenerate the database by running the following command:
Update-Database -Verbose
Programmatically run migrations
If you’d like to automatically migrate the database after application deployment, just run the following code on application startup (e.g. in your Application_Start()
method):
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DataContext,
SampleEfMigrationsApplication.Data.Migrations.Configuration>());
This way the production database is always compatible with the deployed application.
Rico Suter
SOFTWARE ENGINEERING
EDIT
.NET C# Entity Framework Entity Framework Code First Entity Framework Code First Migrations Entity Framework Core Microsoft SQL Server SQL