Start using Entity Framework from the beginning

Macarena Hevia
4 min readFeb 13, 2019

--

Introduction to Entity Framework

Entity Framework is a technology provided by ASP.NET for accessing and interacting with a database, it is an Object Relational Mapper (ORM) that automates mapping between Classes and Tables.

It is very powerful because it simplifies the developer’s work making it more productive. Various things can be automated such as: database/tables creation, update, deletion, opening and closing connections, creation of DataSets, object conversion, application of business rules, etc.

Entity Framework has three different approaches to work with, 1) Database First, 2) Code First, 3) Model First. For the purpose of this tutorial I will focus on the Code First approach.

What you need to install

The first and most important requirement to use Entity Framework is to install Visual Studio, the latest version is Visual Studio 2017 and you can download the free version (Community) from here.

It is also necessary to have a database to work with, I recommend to download SQL Server Express from here.

That is all you need to start your first Entity Framework project.

Starting from scratch

Open Visual Studio 2017 and create a new project, for the purpose of this tutorial we will create a new Console App (.NET Framework) but note that Entity Framework can be used with any project like an MVC Web Application.

After creating the project, you will see something like this:

Now we are ready to add some classes that will then be mapped to our database. With Code First approach we don’t need to create the database to start working with it, Entity Framework will take care of that on our first migration.

I will create two classes, Book and Genre, this database will have a catalogue of books that will each be related to one genre.

The classes will look something like this:

The Book class has one Genre and a Genre can be on many Books, we represent a one-to-many relationship by adding a Genre property to Book and a list of Books to Genre.

Now we need to add a connection string to our App.config, depending on the database you are using, it should look like this:

<connectionStrings><add name=”DefaultConnection” connectionString=”data         source=ELF7IT218958\SQLEXPRESS; initial catalog=BookShop; integrated security=SSPI” providerName=”System.Data.SqlClient” /></connectionStrings>

I named the connection “DefaultConnection” and the database “BookShop”, this is where Entity Framework will look at when first creating the database and assign it its name.

At this point we need to actually install Entity Framework in the project so we can start using their libraries. To do it we will use Package Manager Console, it is under the Tools menu — NuGet Package Manager — Package Manager Console. Enter the following command:

Install-Package EntityFramework

We will now need a class that is going to be our context and extend DbContext so that Entity Framework can know which classes will be mapped to tables and monitor all the changes made to them.

BookContext extends DbContext which is imported using System.Data.Entity, in the constructor we can indicate the name of the connection string, if the name is the same as the class name we can omit this part. And we have to create a DbSet for each class.

Now we have everything we need to create the database, the final steps are enabling migrations, creating a migration and updating the database, they are made in the Package Manager Console.

Enable-Migrations

This command will create a Migrations folder in your project which is going to store all the migrations.

We will add a migration that will create our database and tables automatically, every time we make a change to our models we should add a migration, I just decided to name it InitialMigration, this name should be very descriptive since migrations should be easily identified if we want to go back to any point previous to changes.

Add-Migration InitialMigration

The migration code was auto-generated and looks like this:

Every migration needs to update the database, we cannot create more than one migration and try to update both.

Update-Database

If we look into SQL Server, our database was successfully created with the two tables.

Every time we make a change on the Genre or Book classes and we run the command Add-Migration Entity Framework will know which changes were made and add them to the migration code accordingly.

Now we are all set and using Entity Framework Code First approach, it was a lot faster and easier than creating both the database and classes separately.

This tutorial only shows the creation of a new database, of course Entity Framework is way more powerful than just that, but the purpose of this basic tutorial was just to get your environment setup and a small demo of the framework.

References

https://www.c-sharpcorner.com/blogs/introduction-to-entity-framework-and-entity-framework-core

--

--