ASP.NET Core Web API Entity Framework Scaffolding
Contents
In this article, we will apply the scaffolding technique in the ASP.NET Core Web API that we have created before, and add the existing database to the project and perform CRUD operations. By using scaffolding, you can perform CRUD operations in the database more quickly.
Before continuing, I would like to take a look at Create And Consume ASP.NET Core Web API and the ASP.NET Core Web API Application Structure articles to understand the project structure.
In this tutorial, we will first add the MS SQL database through Server Explorer, then install the necessary plugins and quickly create our Model classes with Scaffolding and perform CRUD operations. Let’s start.
Add Data Connection in Server Explorer
You can add it from the Server Explorer window to quickly access the existing database that we created using MS SQL Server Studio. If you can’t see the Server Explorer window, you can open it using the Ctrl + Alt + S shortcut.
First, right-click on Data Connections in the Server Explorer window and select Add Connections. In the window that opens, select the Server Name and the database you want to add and press Ok. If you are using MS SQL Server, you can enter the Server Name as .\SQLEXPRESS.
If you have not yet created a database and added data, you can review the articles Creating Microsoft SQL Server Database and Manually Database Operations in SQL Server.
Add EntityFramework Plugins
Now let’s install the EntitiyFramework plug-ins required in our project while performing the scaffolding process. Right click on the project and go to Manage NuGet Packages. Add the following plugins to the project.
Or, you can add plugins via the Package Manager Console by typing the following commands.
Install-Package Microsoft.EntityFrameworkCore.SqlServer -Version 5.0.0-rc.1.20451.13
Install-Package Microsoft.EntityFrameworkCore.Design -Version 5.0.0-rc.1.20451.13
Install-Package Microsoft.EntityFrameworkCore.Tools -Version 5.0.0-rc.1.20451.13
Scaffolding Operation
Now it’s time to create entity and context classes by performing scaffolding. To apply scaffolding, enter and run the following command in Package Manager Console.
Scaffold-DbContext "YourConnectionString" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
Here, write your own connection string in “Your ConnectionString” section.
After running this command you will see that Model classes have been added to the project. As you can see, scaffolding saved us time.
Add Connection String to appsettings.json
We created our model classes with scaffolding, but we are still not done. We need to add the connection string we entered while scaffolding to appsettings.json file. Add the following line to the appsettings.json file.
"ConnectionStrings": {
"Database": "Server=.\\sqlexpress;Initial Catalog=Palette;Integrated Security=True"
}
Ultimately, the attached version will be like this,
Add DbContext to Startup.cs
We need to add our DbContext to the Startup class before adding the Controllers class. Otherwise, if we run the project without adding DbContext, we get runtime error.
Go to the Startup.cs class and paste the following line into the ConfigureServices method. So much.
services.AddDbContext<YourContext>(op => op.UseSqlServer(Configuration.GetConnectionString("Database")));
Write your own Context in “YourContext”.
Add Controllers
Eventually, we have come to the last step. Right-click on the Controllers folder and go to Add> Controllers. Here, we can quickly create our controller class from API Controller with actions, using Entity Framework. In the window that opens, create a controller by entering your model class, context class and controller name.
Conclusion
And finally our project is ready. Run the project and change the address bar as below.