Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
83 views14 pages

Aspdotnet MVC Exercise Step1-4

This document provides steps for creating an ASP.NET MVC CRUD application using Entity Framework Core and code first migrations. It involves setting up a SQL Server database, creating an MVC project, installing NuGet packages like EntityFrameworkCore.SqlServer and EntityFrameworkCore.Tools, and using scaffolding commands in the package manager console to generate models and controller code based on the database schema.

Uploaded by

Azfar Jiji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views14 pages

Aspdotnet MVC Exercise Step1-4

This document provides steps for creating an ASP.NET MVC CRUD application using Entity Framework Core and code first migrations. It involves setting up a SQL Server database, creating an MVC project, installing NuGet packages like EntityFrameworkCore.SqlServer and EntityFrameworkCore.Tools, and using scaffolding commands in the package manager console to generate models and controller code based on the database schema.

Uploaded by

Azfar Jiji
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

ASP.

NET MVC CRUD EXERCISE

STEP 1: DB First using SQL Server Management Studio 2019


STEP 2: Create New ASP.NET MVC Project
ASP.NET Core apps created with the web templates contain the application startup code in the
Program. cs file. The Program. cs file is where: Services required by the app are configured
Program.cs is the entry Point for application. Like any application the execution of Application
starts from public static void Main(string[] args){} This Program. cs creates the application Host.
It configures the setings file like appsettings.
STEP 3: Manage NuGet Packages
An essential tool for any modern development platform is a mechanism through which
developers can create, share, and consume useful code. Often such code is bundled into
"packages" that contain compiled code (as DLLs) along with other content needed in the
projects that consume these packages.
For .NET (including .NET Core), the Microsoft-supported mechanism for sharing code is NuGet,
which defines how packages for .NET are created, hosted, and consumed, and provides as the
tools for each of those roles.
NuGet (pronounced "New Get") is a package manager designed to enable developers to
share reusable code. It is a software as a service solution whose client app is free and open-
source. The Outercurve Foundation initially created it under the name NuPack.
In its role as a public host, NuGet itself maintains the central repository of over 100,000 unique
packages at nuget.org. These packages are employed by millions of .NET/.NET Core
developers every day. NuGet also enables you to host packages privately in the cloud (such as
on Azure DevOps), on a private network, or even on just your local file system. By doing so,
those packages are available to only those developers that have access to the host, giving you
the ability to make packages available to a specific group of consumers. The options are
explained on Hosting your own NuGet feeds. Through configuration options, you can also
control exactly which hosts can be accessed by any given computer, thereby ensuring that
packages are obtained from specific sources rather than a public repository like nuget.org.
Put simply, a NuGet package is a single ZIP file with the .nupkg extension that contains
compiled code (DLLs), other files related to that code, and a descriptive manifest that
includes information like the package's version number. Developers with code to share create
packages and publish them to a public or private host. Package consumers obtain those
packages from suitable hosts, add them to their projects, and then call a package's
functionality in their project code. NuGet itself then handles all of the intermediate details.
Because NuGet supports private hosts alongside the public nuget.org host, you can use NuGet
packages to share code that's exclusive to an organization or a work group. You can also use
NuGet packages as a convenient way to factor your own code for use in nothing but your own
projects. In short, a NuGet package is a shareable unit of code, but does not require nor
imply any particular means of sharing. More about NuGeT is at https://learn.microsoft.com/en-
us/nuget/what-is-nuget. Create and publish NuGet at:
https://learn.microsoft.com/en-us/nuget/quickstart/create-and-publish-a-package-using-visual-
studio?tabs=netcore-cli
Right click project name in the solution provider. Choose Manage NuGet Packages. After
the NuGet Package Manager pane opens, you may choose Browse and type in the package
name. In this exercise we are going to install 3 packages:
i. Microsoft.EntityFrameworkCore.SqlServer
ii. Microsoft.EntityFrameworkCore.Tools
iii. Microsoft.Extensions.Configuration

Entity Framework (EF) Core is a lightweight, extensible, open source and cross-platform version


of the popular Entity Framework data access technology.
EF Core can serve as an object-relational mapper (O/RM), which:
 Enables .NET developers to work with a database using .NET objects.
 Eliminates the need for most of the data-access code that typically needs to be written.
More info of EF at: https://learn.microsoft.com/en-us/ef/core/?source=recommendations
Install Microsoft.EntityFrameworkCore.SqlServer

The Microsoft.EntityFrameworkCore.SqlServer is a Microsoft SQL Server database provider for


Entity Framework Core. It is a modern object-database mapper for .NET. It supports LINQ
queries, change tracking, updates, and schema migrations. EF Core works with SQL Server,
Azure SQL Database, SQLite, Azure Cosmos DB, MySQL, PostgreSQL, and other databases
through a provider plugin API. More info
https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.SqlServer/#usedby-body-tab
Install Microsoft.EntityFrameworkCore.Tools

The Entity Framework Core tools help with design-time development tasks. They're primarily
used to manage Migrations and to scaffold a DbContext and entity types by reverse
engineering the schema of a database. More info
https://www.nuget.org/packages/Microsoft.EntityFrameworkCore.Tools#readme-body-tab
Install Microsoft.Extensions.Configuration

Implementation of key-value pair based configuration for Microsoft.Extensions.Configuration.


Includes the memory configuration provider. More info at:
https://www.nuget.org/packages/Microsoft.Extensions.Configuration/
Upon completed, check all the packages under Packages folder in the Solution Explorer
STEP 4: Package Manager Console
From Visual Studio, select Tools > NuGet Package Manager > Package Manager Console.
After the Package Manager Console pane opens, verify that the Default project drop-down list
shows the project in which you want to install the package. This shall display the console.
We are going to run scaffolding code here. ASP.NET Scaffolding is a code generation
framework for ASP.NET Web applications that automatically adds codes and creates
view pages and controllers for your project. Visual Studio includes pre-installed code
generators for MVC and Web API projects. Scaffolding makes developer job easier by creating
controllers and view pages for the data model. It generates codes and pages for CRUD(Create,
Read, Update and Delete) Operation. You add scaffolding to your project when you want to
quickly add code that interacts with data models.

Syntax:
Scaffold-DbContext [-Connection] [-Provider] [-OutputDir] [-Context] [-Schemas>] [-
Tables>]
[-DataAnnotations] [-Force] [-Project] [-StartupProject]
[<CommonParameters>

Scaffold-DbContext "Server=.\
SQLExpress;Database=HumanResource;Trusted_Connection=True;"
Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

Exercise scaffolding https://www.youtube.com/watch?v=SnU4Ulee_NI

You might also like