Create a ASP.NET Core Web Application (.NET Core) project and select the template Web Application.
In this example I will create a simple Web Application to model a simple school. I will call the project SchoolWebApp.
Install the packages for EF Core tools and SqlServer provider as seen in the previous post.
Create the model
Create a Model folder. Inside this folder, create a Student.cs file and will insert the following properties:
Add also the using Microsoft.EntityFrameworkCore; at the beginning of the file.
Generate the code to create the database and execute it
We are finally ready to automatically generate the code, which once executed will create the database.
Open the Package Manager Console and enter the command Add-Migration followed by the name you want to give to this migration. For example:
Let’s look at the project directory: a new Migration folder has been generated.
It contains 2 classes:
Analyze the content of the first file.
It contains a class CreateStudentsMigration (the name is the same of the one entered as argument of Add-Migration command), which has 2 methods:
The Up method is responsible of the creation of the Students table in the database. It creates the table, the columns and the contraints inferred by the class model.
The Down method is responsible to delete the table once it’s not longer needed.
Now it’s time to generate the database. With EF core is a very simple operation, you only have to run the
command to apply the migration.
View the created database
Of course, we are very interested in viewing the created database.
To do so, open the SQL Server Object Explorer tab in Visual Studio, expand the (localdb)\MSSQLLocalDB selection, Database folder and… here’s our brand new SchoolDatabase.
Expand it, go into the Tables folder and you can find the dbo.Students table!
Double clicking on the dbo.Students table it will open the Design window. Here you can see the table structure and the SQL commands that created it.
Well done! We have generated the relational database from our class model!
Adding a class to the model
In the real world, a student belongs to a class. The class is characterized by a name, a room used for the lecture and, of course, by a set of students.
Let’s add a class to the model which represents this concept.
Add to the Model folder the Class.cs file and insert the following code:
Now we can apply these changes to the database running:
As you can see, EF automatically infers the 1:N relation between Students and Classes and generates the foreign key in the Students table. That’s very elegant!
Undoing a database update
What can we do if the migration we have applied was wrong?
Fortunately, EF provides also this scenario.
If you want to undo the Update-Database command, you can just run a Update-Database followed by the name of the migration you want to rollback.
For example, if we want to come back to the previous state, when we had only the Students table, just run:
Now it’s time to generate the database. With EF core is a very simple operation, you only have to run:
Entity Framework Core is the brand new ORM framework that allows a .NET Core developer to work with a database without worrying about writing data-access code and mapping the object model with the relational model.
It has also some features that makes it more than a simple ORM: with EF Core , a developer can generate the relational model given the object model, or viceversa.
With the code-first approach, once you have generated the Model following certain conventions, you will be able to automatically generate the entire database and map the model classes with the generated relational tables.
To use the EF Core with this approach, we need to install the packages which provide the EF commands and the database provider (in this example we will use SqlServer).
We can procede using the Nuget Package Manager Console, but we can use also the NuGet GUI window.
The packages to be installed are:
Before installing a package, we need to check the dependencies that it requires.
Running the Install-Package Microsoft.EntityFrameworkCore.SqlServer command, it will install the latest version of the package (currently 1.1.0).
This package depends on other packages with version >= 1.1.0, but in my environment I have installed the LTS version 1.0.1. We have an issue.
There are 2 possible solutions:
update the .NET core environment to the non LTS version
install the Microsoft.EntityFrameworkCore.SqlServer package selecting a version which requires dependencies >= 1.0.1
In this case, I prefer to mantain the LTS version of .NET Core framework (1.0.1), so I’m going to install an ‘old’ version of the Microsoft.EntityFrameworkCore.SqlServer package.
Looking at its NuGet page, in the Version History section, I can see all its versions.
Clicking on one version, its detailed page will be shown, so I can check the version required for each dependency.
Once I have finished to check the dependencies, I have found that the version 1.0.1 of the Microsoft.EntityFrameworkCore.SqlServer is the latest compatible, so I choose to install it.
To do so, I have to run on the Nuget Package Manager Console: Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 1.0.1
After a few seconds (once the Restoring packages label on the top of Solution Explorer tab fades away), the package is installed in the selected project (the one selected in the Default project comboBox in the Package Manager Console tab).
Now, let’s do the same operations also for Microsoft.EntityFrameworkCore.Tools and Microsoft.EntityFrameworkCore.Design packages.
Run the following commands in the Nuget Package Manager Console: