Open Visual Studio and create a new project either from the File
menu or from the Start
page.
From the New Project
section, create a new project using any one of the following approaches:
- Select
Create new project...
. On the left pane, selectTemplates
|Visual C#
|.NET
C
ore
. Select theASP.NET Core Web Application template from the list.
- Search the project templates for the
ASP.NET Core Web Application
and select it. As displayed in the following screenshot, enterMasteringEFCore.Web
as theName
andMasteringEFCore
as theSolution name
and clickOK
:
New project
From the File menu, perform the following steps:
- Select
|New
.Project
- On the left pane, select
|Templates |
Visual C#
..NET Core
- Select the
ASP.NET Core Web Application template from the list.
- As displayed in the previous screenshot, enter
MasteringEFCore.CodeFirst.Starter
as theName
andMasteringEFCore
as theSolution name
and clickOK
.
Irrespective of the previous two approaches, the selected template will provide New ASP.NET Web Application (.NET Core)
dialog, to let us choose from the following:
Empty
Web API
: Creates a Web API projectWeb Application (Model-View-Controller)
: Creates an MVC Web application which also allows us to create APIs
We will be selecting Web Application (Model-View-Controller)
from the dialog as shown here:
New ASP.NET web project dialog
- In our case, select
.NET Core
,ASP.NET Core 2.0
, and theWeb Application (Model-View-Controller)
template, and also keep the Authentication set toNo Authentication
. ClickOK
:
ASP.NET Core web application
The generated web application displays a tabbed interface which is new to us (instead of displaying index.cshtml
). It allows us to access documentation, connect to any service or even decide on publishing options right from the start page.
Note
If we look closely, we will notice that Visual Studio was silently restoring the packages, and almost everything was part of a package in .NET Core. No more heavyweight framework which always loads tons of DLLs even though we don't require them! Now everything is broken into lighter packages which we could utilize based on our requirements.
I know getting into MVC would be a little outside of the scope of this chapter, but let's dig into a few details before we deep dive into the Entity Framework.
A .NET Core web application is composed of the following folders:
Dependencies
: SDK, server, and client-side dependencieswwwroot
: All static resources should reside hereConnected Services
: To connect external services available in MarketplacelaunchSettings.json
: Settings required to launch a web applicationappSettings.json
: Configurations such as logging and connection stringsbower.json
: Client-side dependencies should be configured herebundleConfig.json
: Bundling is moved to the JSON configuration nowProgram.cs
: Everything starts fromMain()
and any program can be made into a web application using theWebHostBuilder
APIStartup.cs
: For adding and configuring startup services like MVC support, logging, static files support and so onControllers
,Views
: Part of MVC and contains actions and corresponding views
The structure we had discussed so far is illustrated in the following screenshot:
ASP.NET Core Web Application structure
The following highlighted sections in Views\Shared\_Layout.cshtml
should be modified with the desired application name:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewData["Title"] - MasteringEFCore.Web</title> ... </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container"> <div class="navbar-header"> ... <a asp-area="" asp-controller="Home" asp-action="Index" class="navbar-brand">MasteringEFCore.Web</a> ... <div class="container body-content"> ... <footer> <p>© 2017 - MasteringEFCore.Web</p> </footer> ... </body>
We have created a .NET Core web application with no authentication and explored the structure of the project, which might help us understand MVC in .NET Core. If we expand the dependencies, it is evident that we don't have built-in support for Entity Framework (EF) Core. We will look at the different ways of identifying and installing the packages.