I am writing about .NET EF core since past few weeks and if you have followed the series, you may already have seen the article about what are migrations and how they can be generated.
In this article, we are going to have a look at possible alternative which may be used instead of migrations.
What are migrations ?
Most of the real world applications use some kind of data store to save the data. Similar data is categorized and saved in same table (if it is relational database).
With EF Core Code First approach, all the data entities can be written using C#. This C# code is then applied to the underlying database via migrations.
Migration is a feature which allows to keep the C# code in sync with database, while also preserving the data in the database. When migrations are generated they generate a code (.CS files) which tells how new changes can be applied to the existing database.
EF core keeps track of already applied migrations via a special table – __EFMigrationsHistory.
What is the alternative ?
Migrations are beneficial and they are recommended over other alternatives. But what if the project has just started or you are working on a proof of concept, you do not want to use the migrations yet ?
The lightweight alternative – especially when the schema can be completely dropped and new database can be created – is to use Create and Drop APIs provided by EF core. These APIs provide two methods:
- EnsureCreated or EnsureCreatedAsync – to create the database
- EnsureDeleted or EnsureDeletedAsync – to delete the database. It will delete all the tables and all the data from those tables.
One more reason to use those methods can be the data providers. Some data providers may not support migrations. In that case these APIs are the only choice for applying schema changes to the database.
We are going to use the same code that we have created in previous posts (EF Core Interceptors Demo from my GitHub repository).
Now, we have a UniversityContext, an entity – Student and a console app which uses these two to insert new students in the database.
If you already have created the database using EF core migrations, delete the database from SQL Server. Now change the console app code to use the above mentioned APIs to drop and create a new database as shown in below snippet.
Note that EnsureCreated or EnsureCreatedAsync creates new database only if the database is not already created. If it chooses to create the database, it will also create the schema defined in the DbContext. But if database is already present then schema changes would not be applied.
The database created using this method would not have the migrations history table ( __EFMigrationsHistory ).
As per documentation, it is not recommended to use both the approaches ( these create and drop APIs with migrations) simultaneously.
Also, if you started POC with these alternative APIs, it might not be seamless experience to migrate from these APIs to migration. So, for example, if you have a database created using EnsureCreated during POC mode and now suddenly you want to use migrations, it is always recommended to drop the existing database first and then create migrations.
Also, if you want to use these alternative APIs, you should note that they should be called before accessing any entities from database or before performing any other database related operation.
I hope you find this information helpful. Let me know your thoughts.