Creating CRUD (Create/Read/Update/Delete) operations manually would take quite a long time. It's a repetitive operation that could be automated. The process of automating this CRUD operation is referred to as scaffolding:
- Right-click on the
Controllers
folder and selectAdd
|New Scaffolded Item
. - A dialog box will be shown to
Add MVC Dependencies
. - Select
Minimal Dependencies
from the dialog box. Visual Studio adds the NuGet packages required to scaffold the MVC Controller and includes theMicrosoft.EntityFrameworkCore.Design
and theMicrosoft.EntityFrameworkCore.SqlServer.Design
packages. It also includesScaffoldingReadme.txt
, which is not required. We could just delete it.
Note
Once the minimal setup is completed, we need to build/rebuild the application otherwise the same Add MVC Dependencies
dialog will be displayed instead of the Add Scaffold
dialog.
At this point, the tools required to scaffold Controller
and View
are included by Visual Studio, and we are ready to start the process of scaffolding again:
- Right-click on the
Controllers
folder and selectAdd
|New Scaffolded Item
- In the
Add Scaffold
dialog, selectMVC Controller with views, using Entity Framework
as follows:
- In the
Add Controller
dialog, select the appropriateModel
andData context class
(Blog
andBlogContext
in our case), along with theBlogsController
auto-generated controller name:
- Click
Add,
shown as follows:
Scaffolded items
- The scaffolded code includes the CRUD operation in the MVC
Controllers
andViews
. Examining the scaffolded MVC code would be out of the scope of this chapter, so we will focus on the EF scaffolded part alone:
public class BlogsController : Controller { private readonly BlogContext _context; public BlogsController(BlogContext context) { _context = context; } // GET: Blogs public async Task<IActionResult> Index() { return View(await _context.Blogs.ToListAsync()); } ... }
- In the preceding code block, you may notice that the dependency injection was used when passing the
BlogContext
(MasteringEFCoreBlog
database context) to the controller, which was also used in theIndex()
action:
<div class="navbar-collapse collapse"> <ul class="nav navbar-nav"> <li><a asp-area="" asp-controller="Home" asp-action="Index">Home</a></li> <li><a asp-area="" asp-controller="Blogs" asp-action="Index">Blogs</a></li> ...
- We need to update the navigation, as displayed in the preceding code, in
Views\Shared\_Layout.cshtml
, without which we won't be able to view the CRUD operations in theBlogs
module. All set. Let's run and see the CRUD operations in action:
Updated navigation menu with Blogs
The preceding screenshot is the home page of the ASP.NET Core web application. We have highlighted the Blogs
hyperlink in the navigation menu. The Blogs
hyperlink would take the user to the Index
page, which would list all the blog items:
Blogs list
Let's try to create a blog entry in the system, as follows:
Creating a Blog
The Create
page provides input elements required to populate the entity which needs to be created, so let's provide the required data and verify it:
Blog detail page
The Details
page displays the entity, and the preceding screenshot displays the entity that was just created. The Edit page provides input elements required and also pre-populates with existing data, which could be edited by using and updating the data:
Editing a Blog
The Delete page provides a confirmation view that lets the users confirm whether or not they would like to delete the item:
Deleting a Blog
Note
This Delete
page will be displayed when the user selects the Delete
hyperlink in the item row on the list page. Instead of deleting the blog directly from the action, we will be routing the user to the Delete
page to get confirmation before performing the action.
We have identified how to perform CRUD operations using EF Core; since exploring MVC was out of the scope of this book. We stuck to analyzing scaffolding related to EF only.