0% found this document useful (0 votes)
20 views

Bhatti WebTechnology With .NET Unit-3

Uploaded by

kihete5542
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Bhatti WebTechnology With .NET Unit-3

Uploaded by

kihete5542
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 55

2023 Lecture Notes of Web Technology

with .NET

Web Technology with


.NET

Working with Data

Prof. (Dr.) Dharmendra Bhatti 1

Cache in computing

lIn computing, a cache is a hardware or


software component that stores data so
that future requests for that data can be
served faster.
lThe data stored in a cache might be the
result of an earlier computation or a copy
of data stored elsewhere.

Prof. (Dr.) Dharmendra Bhatti 2

Prof. (Dr.) Dharmendra Bhatti 1


2023 Lecture Notes of Web Technology
with .NET

Cache in computing

lA cache hit occurs when the requested


data can be found in a cache, while
a cache miss occurs when it cannot.
lCache hits are served by reading data
from the cache, which is faster than
recomputing a result or reading from a
slower data store; thus, the more requests
that can be served from the cache, the
faster the system performs.
Prof. (Dr.) Dharmendra Bhatti 3

Cache in computing

Prof. (Dr.) Dharmendra Bhatti 4

Prof. (Dr.) Dharmendra Bhatti 2


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lCaching means to store something in


memory/storage that is being used
frequently to provide better performance.

Prof. (Dr.) Dharmendra Bhatti 5

Caching in ASP.NET Core MVC

Prof. (Dr.) Dharmendra Bhatti 6

Prof. (Dr.) Dharmendra Bhatti 3


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lCaching refers to the process of storing


frequently used data so that those data
can be served much faster for
subsequent future requests.

Prof. (Dr.) Dharmendra Bhatti 7

Caching in ASP.NET Core MVC

lCaching can significantly improve the


performance and scalability of an app by
reducing the work required to generate
content.

lCaching works best with data that


changes infrequently and is expensive to
generate.

Prof. (Dr.) Dharmendra Bhatti 8

Prof. (Dr.) Dharmendra Bhatti 4


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lCaching makes a copy of data that can be


returned much faster than from the source.

lApps should be written and tested


to never depend on cached data.

Prof. (Dr.) Dharmendra Bhatti 9

Caching in ASP.NET Core MVC

lASP.NET Core supports different caches:

lIMemoryCache

lIDistributedCache

Prof. (Dr.) Dharmendra Bhatti 10

Prof. (Dr.) Dharmendra Bhatti 5


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lThe simplest cache is based on the


IMemoryCache.

lIMemoryCache represents a cache stored


in the memory of the web server.

Prof. (Dr.) Dharmendra Bhatti 11

Caching in ASP.NET Core MVC

lApps running on a server farm (multiple


servers) should ensure sessions are sticky
when using the in-memory cache.
lSticky sessions ensure that requests
from a client all go to the same server.
lFor example, Azure Web apps use
Application Request Routing (ARR) to
route all requests to the same server.

Prof. (Dr.) Dharmendra Bhatti 12

Prof. (Dr.) Dharmendra Bhatti 6


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lNon-sticky sessions in a web farm require


a distributed cache to avoid cache
consistency problems.
lFor some apps, a distributed cache can
support higher scale-out than an in-
memory cache.
lUsing a distributed cache offloads the
cache memory to an external process.

Prof. (Dr.) Dharmendra Bhatti 13

Caching in ASP.NET Core MVC

lA distributed cache is a cache shared by


multiple app servers, typically maintained
as an external service to the app servers
that access it.
lA distributed cache can improve the
performance and scalability of an
ASP.NET Core app, especially when the
app is hosted by a cloud service or a
server farm.
Prof. (Dr.) Dharmendra Bhatti 14

Prof. (Dr.) Dharmendra Bhatti 7


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lAdvantages of distributed cache:


¡Is coherent (consistent) across requests to
multiple servers.
¡Survives server restarts and app deployments.
¡Doesn't use local memory.

Prof. (Dr.) Dharmendra Bhatti 15

Caching in ASP.NET Core MVC

lImplementation of Distributed Cache


¡Distributed Redis cache
¡Distributed Memory Cache
¡Distributed SQL Server cache
¡Distributed NCache cache

Prof. (Dr.) Dharmendra Bhatti 16

Prof. (Dr.) Dharmendra Bhatti 8


2023 Lecture Notes of Web Technology
with .NET

Caching in ASP.NET Core MVC

lThe in-memory cache can store any


object.
lThe distributed cache interface is limited
to byte[ ].
lThe in-memory and distributed cache
store cache items as key-value pairs.

Prof. (Dr.) Dharmendra Bhatti 17

Cache guidelines

lCode should always have a fallback option


to fetch data and not depend on a cached
value being available.

Prof. (Dr.) Dharmendra Bhatti 18

Prof. (Dr.) Dharmendra Bhatti 9


2023 Lecture Notes of Web Technology
with .NET

Cache guidelines

lThe cache uses a scarce resource,


memory. Limit cache growth:
¡Do not insert external input into the cache. As
an example, using arbitrary user-provided input
as a cache key is not recommended since the
input might consume an unpredictable amount
of memory.
¡Use expirations to limit cache growth.
¡Use SetSize, Size, and SizeLimit to limit cache
size.
Prof. (Dr.) Dharmendra Bhatti 19

Caching

lCaching allow responses to be produced


using data that has been previously
created, either because it was created for
an earlier request or because it has been
stored in a database.

Prof. (Dr.) Dharmendra Bhatti 20

Prof. (Dr.) Dharmendra Bhatti 10


2023 Lecture Notes of Web Technology
with .NET

Why caching???

lMost web applications deal with data that


is expensive to re-create for every request.

lCaching allow responses to be produced


more efficiently and with fewer resources.

Prof. (Dr.) Dharmendra Bhatti 21

How are they use?

lData values are cached using a service.


Responses are cached by a middleware
component based on the Cache-Control
header.
lDatabases are accessed through a service
that translates LINQ queries into SQL
statements.

Prof. (Dr.) Dharmendra Bhatti 22

Prof. (Dr.) Dharmendra Bhatti 11


2023 Lecture Notes of Web Technology
with .NET

Limitations of Caching???

lFor caching, it is important to test the


effect of your cache policy before
deploying the application to ensure you
have found the right balance between
efficiency and freshness.

Prof. (Dr.) Dharmendra Bhatti 23

Caching Data

lIn many web applications, there will be


some items of data that are relatively
expensive to generate but are required
repeatedly.

lRepeatedly performing the same set of


calculations can increase the resources
required to host the application.

Prof. (Dr.) Dharmendra Bhatti 24

Prof. (Dr.) Dharmendra Bhatti 12


2023 Lecture Notes of Web Technology
with .NET

Example – CacheData1

lIn Program.cs file, add in-memory cache


service:

lbuilder.Services.AddMemoryCache();

Prof. (Dr.) Dharmendra Bhatti 25

Example – CacheData1

lIn HomeController.cs file, add

lusing
Microsoft.Extensions.Caching.Memory;

Prof. (Dr.) Dharmendra Bhatti 26

Prof. (Dr.) Dharmendra Bhatti 13


2023 Lecture Notes of Web Technology
with .NET

Example – CacheData1

lIn HomeController.cs file, use


IMemoryCache interface

Prof. (Dr.) Dharmendra Bhatti 27

Example – CacheData1

Prof. (Dr.) Dharmendra Bhatti 28

Prof. (Dr.) Dharmendra Bhatti 14


2023 Lecture Notes of Web Technology
with .NET

Example – CacheData1

Prof. (Dr.) Dharmendra Bhatti 29

IDistributedCache Methods

Prof. (Dr.) Dharmendra Bhatti 30

Prof. (Dr.) Dharmendra Bhatti 15


2023 Lecture Notes of Web Technology
with .NET

DistributedCacheEntryOptions

lAbsoluteExpiration

lThis property is used to specify an


absolute expiry date.

Prof. (Dr.) Dharmendra Bhatti 31

DistributedCacheEntryOptions

lAbsoluteExpirationRelativeToNow

lThis property is used to specify a relative


expiry date.

Prof. (Dr.) Dharmendra Bhatti 32

Prof. (Dr.) Dharmendra Bhatti 16


2023 Lecture Notes of Web Technology
with .NET

DistributedCacheEntryOptions

lSlidingExpiration

lThis property is used to specify a period of


inactivity, after which the item will be
ejected from the cache if it hasn’t been
read.

Prof. (Dr.) Dharmendra Bhatti 33

MemoryCacheOptions Properties

Prof. (Dr.) Dharmendra Bhatti 34

Prof. (Dr.) Dharmendra Bhatti 17


2023 Lecture Notes of Web Technology
with .NET

MemoryCacheOptions Properties

lIn Program.cs file,

lbuilder.Services.AddDistributedMemoryCa
che(opts => { opts.SizeLimit = 200; });

Prof. (Dr.) Dharmendra Bhatti 35

The Cache Service Implementation


Methods

Prof. (Dr.) Dharmendra Bhatti 36

Prof. (Dr.) Dharmendra Bhatti 18


2023 Lecture Notes of Web Technology
with .NET

Using a Shared and Persistent Data


Cache
lThe cache created by the
AddDistributedMemoryCache method isn’t
distributed.
lThe items are stored in memory as part of
the ASP.NET Core process, which means
that applications that run on multiple
servers or containers don’t share cached
data.
lIt also means that the contents of the cache
are lost when ASP.NET Core is stopped. 37
Prof. (Dr.) Dharmendra Bhatti

Using a Shared and Persistent Data


Cache
lThe AddDistributedSqlServerCache
method stores the cache data in a SQL
Server database, which can be shared
between multiple ASP.NET Core servers
and which stores the data persistently.

Prof. (Dr.) Dharmendra Bhatti 38

Prof. (Dr.) Dharmendra Bhatti 19


2023 Lecture Notes of Web Technology
with .NET

AddDistributedSqlServerCache

lStep-1 Connecting to the Database

lOpen a new PowerShell command prompt


and run the command

lsqlcmd -S "(localdb)\MSSQLLocalDB"

Prof. (Dr.) Dharmendra Bhatti 39

AddDistributedSqlServerCache

lStep-2 Creating the Database

l1> CREATE DATABASE CacheDb


l1> GO

Prof. (Dr.) Dharmendra Bhatti 40

Prof. (Dr.) Dharmendra Bhatti 20


2023 Lecture Notes of Web Technology
with .NET

AddDistributedSqlServerCache

lStep-3 Creating the Cache Database


Table

ldotnet sql-cache create


"Server=(localdb)\MSSQLLocalDB;Databas
e=CacheDb" dbo DataCache

lTable and index were created successfully.


Prof. (Dr.) Dharmendra Bhatti 41

AddDistributedSqlServerCache

lStep-4 Adding a Package to the Project


lOpen a new PowerShell command
prompt, navigate to the project folder, and
run the command
¡dotnet add package
Microsoft.Extensions.Caching.SqlServer --
version 6.0.0
lIf you are using Visual Studio, you can add
the package by selecting Project ➤
Manage NuGetProf.Packages
(Dr.) Dharmendra Bhatti 42

Prof. (Dr.) Dharmendra Bhatti 21


2023 Lecture Notes of Web Technology
with .NET

AddDistributedSqlServerCache

lStep-5 Defining a Connection String in


the appsettings.json File

Prof. (Dr.) Dharmendra Bhatti 43

AddDistributedSqlServerCache

lStep-6 Using a Persistent Data Cache in


the Program.cs File

Prof. (Dr.) Dharmendra Bhatti 44

Prof. (Dr.) Dharmendra Bhatti 22


2023 Lecture Notes of Web Technology
with .NET

SqlServerCacheOptions Properties

Prof. (Dr.) Dharmendra Bhatti 45

Caching Responses

lif a response is expensive to compose and


is likely to be repeated, cache entire
response in place of caching individual
data items.

Prof. (Dr.) Dharmendra Bhatti 46

Prof. (Dr.) Dharmendra Bhatti 23


2023 Lecture Notes of Web Technology
with .NET

Configuring Response Caching in the


Program.cs

Prof. (Dr.) Dharmendra Bhatti 47

Configuring Response Caching in the


Program.cs

lSame response sent by ASP.NET Core for


10 second (configured duration).

Prof. (Dr.) Dharmendra Bhatti 48

Prof. (Dr.) Dharmendra Bhatti 24


2023 Lecture Notes of Web Technology
with .NET

Entity Framework Core

lMany web apps need to access data in


a database.
lEntity Framework Core is well-integrated
into the ASP.NET Core platform, with
good support for creating a database
from C# classes and for creating C#
classes to represent an existing
database.

Prof. (Dr.) Dharmendra Bhatti 49

What is Entity Framework?

lEntity Framework is an object-relational


mapper (O/RM) that enables .NET
developers to work with a database using
.NET objects.
lIt eliminates the need for most of the data-
access code that developers usually need
to write.
lIn other words we can say that it is
bridging the gap between objects and
relational dataProf. (Dr.) Dharmendra Bhatti 50

Prof. (Dr.) Dharmendra Bhatti 25


2023 Lecture Notes of Web Technology
with .NET

What is Entity Framework?

Prof. (Dr.) Dharmendra Bhatti 51

Object Relational Mapping

lObject Relational Mapping (ORM) is a


technique used in creating a "bridge"
between object-oriented programs and, in
most cases, relational databases.

lPut another way, you can see the ORM as


the layer that connects object oriented
programming (OOP) to relational
databases.
Prof. (Dr.) Dharmendra Bhatti 52

Prof. (Dr.) Dharmendra Bhatti 26


2023 Lecture Notes of Web Technology
with .NET

Object Relational Mapping

Prof. (Dr.) Dharmendra Bhatti 53

Object Relational Mapping

lAdvantages
¡It speeds up development time for teams.
¡Decreases the cost of development.
¡Handles the logic required to interact with
databases.
¡Improves security. ORM tools are built to
eliminate the possibility of SQL injection
attacks.
¡You write less code when using ORM tools than
with SQL.
Prof. (Dr.) Dharmendra Bhatti 54

Prof. (Dr.) Dharmendra Bhatti 27


2023 Lecture Notes of Web Technology
with .NET

Object Relational Mapping

lLimitations
¡Learning how to use ORM tools can be time
consuming.
¡They are likely not going to perform better when
very complex queries are involved.
¡ORMs are generally slower than using SQL.

Prof. (Dr.) Dharmendra Bhatti 55

Features of Entity Framework

lCross Platform – EF Core is a cross-


platform framework which can run on Mac,
Linux and Windows.

lModeling - EF (Entity Framework) creates


an EDM (Entity Data Model) based on
POCO (Plain Old CLR Object) entities with
get/set properties of different data types

Prof. (Dr.) Dharmendra Bhatti 56

Prof. (Dr.) Dharmendra Bhatti 28


2023 Lecture Notes of Web Technology
with .NET

Features of Entity Framework

lQuerying - EF allows us to use LINQ


queries (C#/VB.NET) to retrieve data from
the underlying database. EF also allows
us to execute raw SQL queries directly to
the database.
lChange Tracking - EF keeps track of
changes occurred to instances of your
entities (Property values) which need to be
submitted to the database.
Prof. (Dr.) Dharmendra Bhatti 57

Features of Entity Framework

lSaving: EF executes INSERT, UPDATE,


and DELETE commands to the database
based on the changes occurred to your
entities when you call
the SaveChanges() method.
lEF also provides the
asynchronous SaveChangesAsync() meth
od.

Prof. (Dr.) Dharmendra Bhatti 58

Prof. (Dr.) Dharmendra Bhatti 29


2023 Lecture Notes of Web Technology
with .NET

Features of Entity Framework

lCaching: EF includes first level of caching


out of the box. So, repeated querying will
return data from the cache instead of
hitting the database.

Prof. (Dr.) Dharmendra Bhatti 59

Approach

Prof. (Dr.) Dharmendra Bhatti 60

Prof. (Dr.) Dharmendra Bhatti 30


2023 Lecture Notes of Web Technology
with .NET

Approach

lCode First - This approach was


introduced with EF 4.1. It is mainly useful
in Domain Driven Design. In the Code-
First approach, you focus on the domain of
your application and start creating classes
for your domain entity rather than design
your database first and then create the
classes which match your database
design.
Prof. (Dr.) Dharmendra Bhatti 61

Approach

lModel First - In this approach we define


your model in an Entity Framework
designer then generate SQL, which will
create database schema to match your
model and then you execute the SQL to
create the schema in your database.
lThe classes that you interact with in your
application are automatically generated
from the EDMX file.
Prof. (Dr.) Dharmendra Bhatti 62

Prof. (Dr.) Dharmendra Bhatti 31


2023 Lecture Notes of Web Technology
with .NET

Approach

lDB First - The Database First Approach


creates model codes (classes, properties,
DbContext etc.) from the database in the
project and those classes become the link
between the database and controller.

Prof. (Dr.) Dharmendra Bhatti 63

Installing Entity Framework Core

lInstalling the Entity Framework Core


Global Tool Package

ldotnet tool uninstall --global dotnet-ef


ldotnet tool install --global dotnet-ef --
version 6.0.0

Prof. (Dr.) Dharmendra Bhatti 64

Prof. (Dr.) Dharmendra Bhatti 32


2023 Lecture Notes of Web Technology
with .NET

Testing the Entity Framework Core


Global Tool
ldotnet ef --help

Prof. (Dr.) Dharmendra Bhatti 65

Adding Entity Framework Core


Packages to the Project
lEntity Framework Core also requires
packages to be added to the project.
lNavigate to the project folder and run the
commands
¡dotnet add package
Microsoft.EntityFrameworkCore.Design --
version 6.0.0
¡dotnet add package
Microsoft.EntityFrameworkCore.SqlServer --
version 6.0.0
Prof. (Dr.) Dharmendra Bhatti 66

Prof. (Dr.) Dharmendra Bhatti 33


2023 Lecture Notes of Web Technology
with .NET

Creating the Data Model

lThe Calculation.cs File in the


Platform/Models Folder

Prof. (Dr.) Dharmendra Bhatti 67

The Context Class

lEntity Framework Core uses a context


class that provides access to the
database.

Prof. (Dr.) Dharmendra Bhatti 68

Prof. (Dr.) Dharmendra Bhatti 34


2023 Lecture Notes of Web Technology
with .NET

The Context Class

lAdd a file called CalculationContext.cs to


the Platform/Models folder

Prof. (Dr.) Dharmendra Bhatti 69

Configuring the Database Service


lConfiguring the Data Service in the
Program.cs File

Prof. (Dr.) Dharmendra Bhatti 70

Prof. (Dr.) Dharmendra Bhatti 35


2023 Lecture Notes of Web Technology
with .NET

Defining a Connection String


lDefining a Connection String in the
appsettings.json File

Prof. (Dr.) Dharmendra Bhatti 71

Creating and Applying the Database


Migration
lEntity Framework Core manages the
relationship between data model classes
and the database using a feature called
migrations.

Prof. (Dr.) Dharmendra Bhatti 72

Prof. (Dr.) Dharmendra Bhatti 36


2023 Lecture Notes of Web Technology
with .NET

Creating and Applying the Database


Migration
lTo create the initial migration, open a new
PowerShell command prompt at project
location, and run the command:
¡dotnet ef migrations add Initial

lApplying a Migration
¡dotnet ef database update

Prof. (Dr.) Dharmendra Bhatti 73

Seeding the Database


lCreate a file called SeedData.cs in the
Platform/Models folder

Prof. (Dr.) Dharmendra Bhatti 74

Prof. (Dr.) Dharmendra Bhatti 37


2023 Lecture Notes of Web Technology
with .NET

Seeding the Database

Prof. (Dr.) Dharmendra Bhatti 75

Enabling Database Seeding in the


Program.cs File

Prof. (Dr.) Dharmendra Bhatti 76

Prof. (Dr.) Dharmendra Bhatti 38


2023 Lecture Notes of Web Technology
with .NET

Seeding the Database

ldotnet run INITDB=true

Prof. (Dr.) Dharmendra Bhatti 77

Using Data in an Endpoint

lEndpoints and middleware components


access Entity Framework Core data by
declaring a dependency
on the context class and using its
DbSet<T> properties to perform LINQ
queries.

Prof. (Dr.) Dharmendra Bhatti 78

Prof. (Dr.) Dharmendra Bhatti 39


2023 Lecture Notes of Web Technology
with .NET

Using Data in an Endpoint

lThe LINQ queries are translated into SQL


and sent to the database.
lThe row data received from the database
is used to create data model objects that
are used to produce responses.

Prof. (Dr.) Dharmendra Bhatti 79

Using a Database in the


SumEndpoint.cs File

Prof. (Dr.) Dharmendra Bhatti 80

Prof. (Dr.) Dharmendra Bhatti 40


2023 Lecture Notes of Web Technology
with .NET

Enabling Sensitive Data Logging

lEntity Framework Core doesn’t include


parameter values in the logging messages
it produces, which is why the logging
output contains question marks:

Prof. (Dr.) Dharmendra Bhatti 81

Enabling Sensitive Data Logging

lEnabling Sensitive Data Logging in the


Program.cs File

Prof. (Dr.) Dharmendra Bhatti 82

Prof. (Dr.) Dharmendra Bhatti 41


2023 Lecture Notes of Web Technology
with .NET

WebApp Example

lCreating the Project

Prof. (Dr.) Dharmendra Bhatti 83

Adding a Data Model

lCreate a simple data model and use it to


create the database schema that will be
used to store the application’s data.

Prof. (Dr.) Dharmendra Bhatti 84

Prof. (Dr.) Dharmendra Bhatti 42


2023 Lecture Notes of Web Technology
with .NET

Adding NuGet Packages to the


Project
ldotnet add package
Microsoft.EntityFrameworkCore.Design --
version 7.0.10
ldotnet add package
Microsoft.EntityFrameworkCore.SqlServer
--version 7.0.0
lOR
ldotnet add package
Pomelo.EntityFrameworkCore.MySql --
Prof. (Dr.) Dharmendra Bhatti 85
version 7.0.0

Adding NuGet Packages to the


Project
lIf you are using Visual Studio, you can add
the packages by selecting Project ➤
Manage NuGet Packages.

Prof. (Dr.) Dharmendra Bhatti 86

Prof. (Dr.) Dharmendra Bhatti 43


2023 Lecture Notes of Web Technology
with .NET

Installing a Global Tool Package

ldotnet tool uninstall --global dotnet-ef

ldotnet tool install --global dotnet-ef --


version 7.0.10

Prof. (Dr.) Dharmendra Bhatti 87

Creating the Data Model

lCreate a new class file named


Category.cs in Models folder.

Prof. (Dr.) Dharmendra Bhatti 88

Prof. (Dr.) Dharmendra Bhatti 44


2023 Lecture Notes of Web Technology
with .NET

Creating the Data Model

lCreate a new class file named Supplier.cs


in Models folder.

Prof. (Dr.) Dharmendra Bhatti 89

Creating the Data Model

lNext, add a class named Product.cs to the


Models folder.

Prof. (Dr.) Dharmendra Bhatti 90

Prof. (Dr.) Dharmendra Bhatti 45


2023 Lecture Notes of Web Technology
with .NET

Prof. (Dr.) Dharmendra Bhatti 91

WebApp Example

lEach of the three data model classes


defines a key property whose value will be
allocated by the database when new
objects are stored.
lThere are also navigation properties that
will be used to query for related data so
that it will be possible, for example, to
query for all the products in a specific
category.
Prof. (Dr.) Dharmendra Bhatti 92

Prof. (Dr.) Dharmendra Bhatti 46


2023 Lecture Notes of Web Technology
with .NET

WebApp Example

lTo create the Entity Framework Core


context class that will provide access to
the database, add a file called
DataContext.cs to the Models folder.

lThe context class defines properties that


will be used to query the database for
Product, Category, and Supplier data.

Prof. (Dr.) Dharmendra Bhatti 93

WebApp Example

Prof. (Dr.) Dharmendra Bhatti 94

Prof. (Dr.) Dharmendra Bhatti 47


2023 Lecture Notes of Web Technology
with .NET

Preparing the Seed Data

lAdd a class called SeedData.cs to the


Models folder

Prof. (Dr.) Dharmendra Bhatti 95

Preparing the Seed Data

Prof. (Dr.) Dharmendra Bhatti 96

Prof. (Dr.) Dharmendra Bhatti 48


2023 Lecture Notes of Web Technology
with .NET

Preparing the Seed Data

Prof. (Dr.) Dharmendra Bhatti 97

Preparing the Seed Data

lThe static SeedDatabase method ensures


that all pending migrations have been
applied to the database.

lIf the database is empty, it is seeded with


categories, suppliers, and products.

Prof. (Dr.) Dharmendra Bhatti 98

Prof. (Dr.) Dharmendra Bhatti 49


2023 Lecture Notes of Web Technology
with .NET

Preparing the Seed Data

lEntity Framework Core will take care of


mapping the objects into the tables in the
database, and the key properties will be
assigned automatically when the data is
stored.

Prof. (Dr.) Dharmendra Bhatti 99

Configuring Entity Framework Core


Services and Middleware
lMake the changes to the Program.cs file

Prof. (Dr.) Dharmendra Bhatti 100

Prof. (Dr.) Dharmendra Bhatti 50


2023 Lecture Notes of Web Technology
with .NET

Configuring Entity Framework Core


Services and Middleware

Prof. (Dr.) Dharmendra Bhatti 101

Configuring Entity Framework Core


Services and Middleware
lDefining a Connection String in the
appsettings.json File

Prof. (Dr.) Dharmendra Bhatti 102

Prof. (Dr.) Dharmendra Bhatti 51


2023 Lecture Notes of Web Technology
with .NET

Creating and Applying the Migration

lUse a PowerShell command prompt to run


the command

ldotnet ef migrations add Initial

ldotnet ef database update

Prof. (Dr.) Dharmendra Bhatti 103

Adding the CSS Framework

lInstalling the LibMan Tool Package

ldotnet tool uninstall --global


Microsoft.Web.LibraryManager.Cli

ldotnet tool install --global


Microsoft.Web.LibraryManager.Cli --
version 2.1.175
Prof. (Dr.) Dharmendra Bhatti 104

Prof. (Dr.) Dharmendra Bhatti 52


2023 Lecture Notes of Web Technology
with .NET

Adding the CSS Framework

lInstalling the Bootstrap CSS Framework

llibman init -p cdnjs

llibman install [email protected] -d


wwwroot/lib/bootstrap

Prof. (Dr.) Dharmendra Bhatti 105

Configuring the Request Pipeline

lTo define a simple middleware component


that will be used to make sure the example
project has been set up correctly, add a
class file called TestMiddleware.cs to the
WebApp folder

Prof. (Dr.) Dharmendra Bhatti 106

Prof. (Dr.) Dharmendra Bhatti 53


2023 Lecture Notes of Web Technology
with .NET

Configuring the Request Pipeline

Prof. (Dr.) Dharmendra Bhatti 107

Adding a Middleware Component in


the Program.cs File
lvar app = builder.Build();

lapp.UseMiddleware<WebApp.TestMiddl
eware>();

lapp.MapGet("/", () => "Hello World!");

Prof. (Dr.) Dharmendra Bhatti 108

Prof. (Dr.) Dharmendra Bhatti 54


2023 Lecture Notes of Web Technology
with .NET

Running the Example App

Prof. (Dr.) Dharmendra Bhatti 109

Questions ???

Prof. (Dr.) Dharmendra Bhatti 110

Prof. (Dr.) Dharmendra Bhatti 55

You might also like