MVC
Improve EF Core performance with EF Extensions
MVC
What is MVC?
The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. Each of these components is built to handle specific development aspects of an application.
MVC stands for Model, View, and Controller. MVC separates the application into three components
Model: Responsible for maintaining application data and business logic.
View: The user interface of the application, which displays the data.
Controller: Handles users' requests and renders appropriate view with model data.
Create MVC App
To start, we will create an ASP.NET Core web application project. The project type comes with all template files to create a web application before adding anything. Let's open Visual Studio 2019, if you haven't already installed Visual Studio, go to the Visual Studio downloads page to install it for free.
On the start window, choose Create a new project.
On the Create a new project window, enter or type asp.net in the search box. Next, choose C# from the Language list, and then choose Windows from the Platform list. Select the ASP.NET Core Web Application template, and then choose Next.
In the Configure your new project window, type or enter EFCore5InMvcApp in the Project name box and click on the Create button.
In the Create a new ASP.NET Core web application window, verify that ASP.NET Core 5.0 appears in the top drop-down menu. Then, choose ASP.NET Core Web App (Model-View-Controller), including example ASP.NET Core MVC Views and Controllers, and then click on the Create button.
Visual Studio opens your new project and includes the default code files in your project as shown in the Solution Explorer.
Install Entity Framework Core
To use Entity Framework Core we need to install Microsoft.EntityFrameworkCore library. It is available as a nuget package and you can install it using Nuget Package Manager.
In the Package Manager Console window, enter the following command.
For SQL Server LocalDB, which is installed with Visual Studio, we need to install Microsoft.EntityFrameworkCore.SqlServer and will get all the packages required for EF Core.
We also need to install the following NuGet package.
Create a Data Model and Database Context
In Solution Explorer, right-click on the Models folder and choose Add > Class. Enter a class file name Author.cs and add the following code.
Now let's add another entity class Book
and replace the following code.
So let's create a folder in your project by right-clicking on your project in Solution Explorer and click Add > New Folder. Name the folder DAL (Data Access Layer). In that folder, create a new class file named BookStore.cs, and replace the following code.
Register Context Class
To register BookStore
as a service, open Startup.cs
, and call the AddDnContext
in the ConfigureServices
method.
The name of the connection string is passed into the context by calling a method on a DbContextOptionsBuilder
object.
Setup Connection String
For local development, the ASP.NET Core configuration system reads the connection string from the appsettings.json file. So let's add the connection to that file as shown below.
The above connection string specifies that the Entity Framework will use a LocalDB
database named BookStoreDb
.
Initialize Database
The Entity Framework will create an empty database for you. So we need to write a method that's called after the database is created to populate it with test data.
In the DAL folder, add a new class BookStoreInitializer
and replace the following code.
The above code creates a database when needed and loads test data into the new database.
It also checks if there are any authors in the database, and if not, it assumes the database is new and needs to be seeded with test data.
In Program.cs
file, replace the following code in the Main
method.
On application startup, the Main
method does the following operations.
Get a database context instance from the dependency injection container.
Call the seed method, passing to it the context.
Dispose of the context when the seed method is done.
Create Controller and Views
MVC controllers are responsible for responding to requests made against your website. Each browser request is mapped to a particular controller.
To create a controller, right-click the Controllers folder in Solution Explorer, and select Add > Controller...
It will open the Add Scaffold dialog box.
Select MVC Controller with views, using Entity Framework, and then click the Add button.
In the Add MVC Controller with views, using Entity Framework dialog box, select Author (EFCore5InMvcApp.Models) from the Model class and BookStore (EFCore5InMvcApp.DAL) from the Data context class dropdown.
Enter AuthorController (not AuthorsController) as a Controller name and click the Add button.
The scaffolder creates an AuthorController.cs
file and a set of views (.cshtml
files) that work with the controller.
Setup Menu Options
Open Views\Shared\_Layout.cshtml, and add a menu entry for Authors after the Home menu option as shown below.
Press Ctrl+F5 to run the project, click the Authors tab to see the test data.
References
Last updated