Setting up the Entity Framework Core in a Visual Studio Project

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.

For an overview of the framework, see the MSDN documentation page.

Setup EF Core on VS for code-first approach

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:

  • Microsoft.EntityFrameworkCore.SqlServer
  • Microsoft.EntityFrameworkCore.Tools -Pre
  • Microsoft.EntityFrameworkCore.Design

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:

Install-Package Microsoft.EntityFrameworkCore.Tools -Version 1.0.0-preview3-final -Pre
Install-Package Microsoft.EntityFrameworkCore.Design -Version 1.0.1

The last thing required to complete the EF core setup is to add the EF tools to the project.json file (in the root directory of the project).

Open the file and move to the “tool” section. Add between the brackets the following line:

"Microsoft.EntityFrameworkCore.Tools.DotNet": "1.0.0-preview3-final"

Make sure that the version corresponds to the one you have installed some minutes ago.

Setup EF core on VS for database-first approach

If you already have a database, you can generate the model by your database schema.

To setup the EF core framework to work in this way, we have to install the packages required for the code-first approach plus the Microsoft.EntityFrameworkCore.SqlServer.Design

Run

Install-Package Microsoft.EntityFrameworkCore.SqlServer.Design -Version 1.0.1

Marco Mengoli

Marco Mengoli
Software Engineering student at University of Bologna. I love to solve problems and experiment new things. I'm an enthusiast maker, I love to make lots of projects with my Iot boards. I found on electronics a new really enjoyable hobby. My favourite thematic is Software Engineering, in particular the continuous improvement of the quality of the products I develop and also the way I do it. View more in the About page

Configure NuGet to work behind a proxy

Configure NuGet to work behind a proxy Continue reading

OpenCV 3, the Mat object

Published on April 20, 2016