Database First in Asp.NET Core Applications

In this content, we will examine how to use Entity Framework Core – Database First approach in Asp.NET Core applications and how we can perform our works by using existing databases.

Firstly
In order to apply our narratives, we will now watch through an Asp.NET Core MVC application and use the Northwind database to take advantage of the sample process.

Microsoft.EntityFrameworkCore.SqlServer Setup

In order to use the Entity Framework Core – Database First approach, we need to install the Microsoft.EntityFrameworkCore.SqlServer library in our project. To do this, we can add the related library to the project by running the following code.

Database First With Ready Database

After the operations up to this step, we are ready to include the database on SQL Server to our project by using Database First method.

All you need to do is open the Manager Package Manager Console ”window and run the following directive.

When we run the relevant directive, the database in the provider is moved to the project.

In the files in the Solution Explorer window, we will see that the entities are created and the context class is added to them. The point to be considered here is that; As in the standard Entity Framework, there is no ed .edmx dosya file that provides the necessary configuration settings and shows the relationships between the tables, and operations are performed directly on class based objects.

In addition, in order to retrieve the latest status in database updates, it is sufficient to provide the relevant directive with the -force parameter.

Register with Context Dependency Injection

After all these operations, it is time to register our context object with Dependency Injection. In fact, this is a method that we know and apply from our previous articles.

In order to register our context class with Dependency Injection, we write the following code in the “ConfigureServices” method in the Startup file.

Use of

Usage From this point on, the context object is requested by Dependency Injection to perform the necessary database operations on the controller.

As in the screenshot above, we have performed our operations on our existing database in Asp.NET Core applications by using Database First approach.




if you have question do not forget to write from the chat button next to it or from the comment

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *