Adding Migrations On Entity Framework .NET Core

In this post we will look at adding migrations on entity framework .net core applications.

Introduction

In our previous post, we looked at how we can setup our .net core app with entity framework code first approach. We will continue with the same DbContext and User entity that was created in the previous post for this post.

In any application, the data models frequently during the development phase. So, we need to have a mechanism to update the database entities accordingly.

Updating Entities

If we take a look at the definition of the previously created User table, it should look like this:

Since, we did not specify the data types for the columns, by default Entity Framework added default data-types for these columns.

Now, if we want to have specific data-types, we should explicitly define these properties.

One way to do so is using “Data Annotations“.

Let’s use some make use of the annotations to modify our User table.

As you can see, we have made use of annotations like [Key], [Required] and [MaxLength] here. These identifiers are used to declare a column as primary, non-nullable and specific length data-type respectively.

Running Migrations On Entity Framework

Let’s create and run the migration for User entity.

Now if we take a look at our table schema, we shall see much better data types specified.

Adding Foreign-key Relationships

Now that we have learnt to add migrations on an existing entity framework entity, let’s learn how we can add more entities. To complicate things a little, we will add a Foreign Key relationship as well.

Become A Flutter Expert With This Course

Create another entity called Business such that there is a one-to-many relationship between users and businesses. Each user can have multiple businesses.

Here, we have made use of [ForeignKey] annotation.

Let’s create and run the migrations again:

Oops!

If you followed the steps above properly, you might have noticed that our Business table wasn’t created in the database.

The reason being that we forgot to add the DbSet entry in our context model. Let’s correct this by adjusting our context class.

Since we have already created a migration, we need to remove it first and re-create the migration.

Removing A Migration

If after adding a migration you realize that you need to make some changes on it, you can use the command below.

This will remove the latest migration that was added.

Reapply Migration

This will create a new table the database with the following table schema:

Reverting A Migration After Database Update

Sometimes you might need to revert a migration after the database was already updated. In such scenario, you should re-apply the migration that you wish to revert the database to. We can make use of this command for that:

Conclusion

This post covered adding column definitions, foreign key relationships and column updates on entity framework entities in .net core applications. Furthermore, we also looked at adding, removing and reverting migrations.

With this post and the previous post, we have covered most of the use case scenarios for working with Entity Framework entities.

About the author