This article is the continuation of a series started with the first article explaining how to setup SQL server with Entity Framework using .NET CORE.
This post is going to explain how to create a migration script with the use of Entity Framework tools and then we are going to use this migration to create all the tables on our local database.
Entity Framework is an open-source ORM framework for .NET applications supported by Microsoft. It enables developers to work with data using objects of domain specific classes without focusing on the underlying database tables and columns where this data is stored. With the Entity Framework, developers can work at a higher level of abstraction when they deal with data, and can create and maintain data-oriented applications with less code compared with traditional applications.
The sentence above, perfectly summarise the power of this ORM framework. It supports developers in one of the most tedious tasks.
In this chapter we require three different NuGet packages.
- The Database provider
- The Entity Framework tools
All the packages below will be installed using the Package Manager Console. To access it, using Visual Studio please go to:
Tools > NuGet Package Manager > Package Manager Console
The database provider
In our example we are going to use SQL (As we have already defined in the previous posts), but there is a list of available Database Provider
To install our package we will have to type the following command:
Entity Framework Tools
Entity Framework is powerful by itself, but we also have a set of tools to make it even greater.
The tools can be installed by using the following command in Package Manager Console:
Create a migration script
After all of the above steps, creating a migration script is very straight forward.
Using our previously installed packages we can run the following command in the Package Manager Console
During the above step you may receive an error stating:
The term 'add-migration' is not recognized as the name of a cmdlet
If this error appear you just have to close and reopen visual studio.
The code above will create a migration script called InitialMigrationScript and place in a folder called Migrations.
The file is actually pretty readable and it is useful to analyse to be able to modify and create one in the future without the help of the assistant if necessary.
Run a migration
This is our final step. After which, our table will be fully set and ready to be used within our app.
To create the tables, we have to run the migration file previously created using one of the package installed in the initial steps.
To run the migration run the following command in the Package Manager Console
We are not at the end of this little tutorials. The above steps should have allowed us to create a full migration script that can be used to have a consistent database schemer across environment and to developers plenty of time.
I hope this can be helpful to an of you, as I personally spent some time to be able to put this together and find all the information I needed.
As mentioned above and in my previous post, I am sharing my finding while I use them while I am using this technologies in side projects, and I am more than happy to receive feedback that can help me improve it.