Entity Framework Code First

WHAT IS ENTİTY FRAMEWORK? HOW TO USE?


What is Entity Framework?

Entity Framework is the ORM tool developed by Microsoft since 2008. So what is ORM? ORM or Object to Relational Mapping is basically a software architecture that allows us to use tables and fields in the database as objects. So, the software developer can develop software without the need for database and SQL commands.


Why Use It?

When making database applications with .NET or any programming language, firstly, database connection is made. Then, the data in the database is taken with SQL commands. The data received is stored in data structures suitable for the programming language and the process is done. Unexpected errors and there may be problems while converting data into data structures convenient for programming language. It also makes it more difficult to develop complex database queries. The main use of ORM tools is to exterminate these and other problems.

ORM tools such as Entity Framework allow processing using the same commands for different data providers (such as SQL Server, MySQL, SQLite).

Advantages of using Entity Framework;
-It allows us to get rid of long and complex SQL codes with CRUD (Create, Read, Update, Delete) operations.
-Eliminates dependency on database.
-It allows us to write object-oriented code in database operations.
-It increases database performance thanks to simpler SQL queries.
-It provides to do more work in less time by shortening the code writing time.


Installing Entity Framework

Entity Framework ORM tool can be installed for a Nuget package in the Visual Studio development environment by right-clicking the project and using the Manage Nuget Packages option. You can select Microsoft.EntityFrameworkCore and download it with the version you want to work with. If you say I want to work only with Entity Framework without Core, you can download it from either the packages section or the Package Manager Console section. You can download it with the "Install-Package EntityFramework" command for the Console part. The example I will show in this article will be with Core.

With the Entity Framework, projects can be developed in 3 different ways. That methods;
-Model First
-Database First
-Code First

In this article, we will analyze the Code First method we have created for our website.


.NET Core Entity Framework Code First Approach

Code First (By writing code): This method is a method we implement by starting to create our classes in the visual studio environment. Our database is derived from these classes. Mapping operations can be done by the software developer by using Attributes while creating classes. By the way, the Mapping process is briefly the event where we determine our constraints in our tables.

Code First (Using existing database): In this method, classes and mapping codes are created by the programmer. Our database is updated according to the status of our classes and modeling.

In the Code First structure, the "class" structures in the programming language correspond to the "table" structures in the database, and the "property" structures to the "column" structures in the database. In addition, thanks to Attributes, Validations can be applied to database structures and specific terms or limitations can be added to the columns.

Thanks to the migration structures, we will be able to quickly reflect our innovations to the physical database on Visual Studio. We say it Migration to reflect the changes we made in the code section to the database.

If we explain the subject through the example, we first create a new .NET Core web project. You can choose the version you want to work with. We'll use 3.1 in our example.


code first1

For the project we have created, we download the Microsoft.EntityFrameworkCore package over Manage Nuget Packages.


code first2

Now let's see how to create a database with Code First and add a table within that database. First of all, I am creating a example table.


code first3

We created our class representing our table. Now let's create the context class that we will connect with the database.


code first4

If you analyze the Context class, it derives from the DbContext class. The reason for this is that you will be able to access the properties of Entity Framework architecture from the inheritance of this class, and also ensure that the corresponding class is a context class. The name we give to the "ExampleDbContext" class is very important for us.

The most basic point in Code First structure is to keep the tables in the database as properties from the Generic structure DbSet type. The property named "Ogrencis" you see above will create the physical reflection of our table named "Ogrenci" in the database.

We designed a database in Code First structure and it is time to create this database. This structure we have created does not know which database will be created on which server. We will make the necessary definition into the "appsettings.json" file.


code first5

We define the context class that we created on the Startup.cs file. We define the DefaultConnection data we have created for ConnectionString. We will need to install the EntityFrameworkCore.SqlServer package to define UseSqlServer on the line specified here.


code first6

After doing these operations, the migration must be added using the "Add-Migration MigrationName" command on the Package Manager Console.


code first7

As you can see, the added migration came ready. Let's update the database now. With the update, the database will appear actively.


code first8 code first9

As a result of these operations, we created our database with Entity Framework Code First.

Writer