Entity Framework Core With the Code First Approach in .Net Core Project
By: Enes Gür
Tags: Mssql, Dotnet Core, Migration, Code First, Entityframework Core

In this article, I will talk about how to building-related tables in MSSQL on the .Net Core application with Entity Framework Core, which is a powerful ORM tool that I used it fondly for a while with the Code First approach.

I am going to explain “What is the ORM”, “What is the Entity Framework and its advangates”, “What is the Code First approach”, “How to build related tables with Entity Frame Core in .Net Core project in a sample application”.

What is the ORM

ORM(Object Relational Mapping) is a technology that convert models to our database tables, properties to columns, objects to a records in the database. ORM tools allows perform database operations for us so we can perform database operations on classes and objects.

As we can see in the picture, ORM makes mapping between OOP and database, It provides us with the convenience of application development in relational databases without dealing with database structures and queries.

What is the Entity Framework

Entity Framework is a .Net based ORM tool developed by Microsoft. Entity Framework is a tool that is a bridge between the application and the database.

Entity Framework contains 3 different methods:

Model First Method

Database First Method

Code First Method

In this post, we will do sample application with Code First Method.

The advantages of Entity Framework:

There is no database dependency.

It has a flexible usage.

It can be switched between database management systems, for example, the database created in MS-SQL can be moved to PL-SQL.

What is the Code First Approach

It is an approach that we perform database operations by writing code. It is created by coding all transactions related to the database. In this approach the power is on the developer :)

Now we can move to build a sample application.

Building Related Tables with Entity Framework Core in .Net Core Project in a Sample Application

We are creating a solution and adding 2 projects (default API and default ClassLibrary) under the solution. My .Net Core version is 3.1.

We will realize our scenario through a “Basketball Application”. The application will have classes that will include different relationships (1:1), (1:m), (m:m)

First of all, we should add Entity Framework Core libraries which we need, from Nuget to our project. We will run these commands in Package Manager Console but be sure that Default project section must be “EfCoreRelations.Data”

Install-Package Microsoft.EntityFrameworkCore -Version 3.1.9

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.Relational

And then we are adding these libraries for Api layer. Be sure that Default project section must be “EfCoreRelations.Api”

Install-Package Microsoft.EntityFrameworkCore -Version 3.1.9

Install-Package Microsoft.EntityFrameworkCore.SqlServer

Install-Package Microsoft.EntityFrameworkCore.Tools

According to our scenario, we will have six classes which are “Team”, ”Coach”, ”Country”, ”League”, ”Player”, ”TeamLeague”.

We will creating them in “Data” layer in “Models” folder.

one to one relationship

We will build a one to one relationship between “Team” and “Coach”. A team has only a coach and a coach works only in a team. We are setting our “Team” and “Coach” classes as follows: