File > New Project > ASP.Net Web APP > [Set Project Name] >
Then Choose [.net core], [.net core 3.1], and [Web App]
this will create new project with the next default files
Include MySQL DB support
Tools > NuGet Package Manager > Package Manager Console
run the following commands in the Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.Tools
MySQL has two drivers, any of them should works perfect
Install-Package Devart.Data.MySql.EFCore
OR
Install-Package Pomelo.EntityFrameworkCore.MySql
Run the next command in Package Manager Console
Add [ViewData] attribute before any public variable allow access it through ViewData dictionary in frontend.
Note that any Model public variable can access also in frontend via the @Model.property
asp-page-handler="Delete" ==> OnPostDelete()
asp-page-handler="Edit" ==> OnPostEdit()
asp-page-handler="View" ==> OnPostView()
var x= HttpContext.Session.GetString("Test String");
Then Choose [.net core], [.net core 3.1], and [Web App]
this will create new project with the next default files
Include MySQL DB support
Tools > NuGet Package Manager > Package Manager Console
run the following commands in the Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore.Tools
MySQL has two drivers, any of them should works perfect
Install-Package Devart.Data.MySql.EFCore
OR
Install-Package Pomelo.EntityFrameworkCore.MySql
Creating a Model Classes From MySQL Database through a command
update
appsettings.json
to contains connection string
:"ConnectionStrings": {
"MyDB": "
User Id=root;Host=localhost;Database=ddl_frontend;"}
the file will be like this after updateRun the next command in Package Manager Console
Scaffold-DbContext -Connection name=MyDB -Provider Pomelo.EntityFrameworkCore.MySql -OutputDir Model -Context "MyDbContext" -DataAnnotations -Force
the command will create DB context class and Entity(Tables) classes
Notes:
each razor page consists of frontend and backend code
and any new razor project contains start page "index.cshtml" and it inherit page design from "Shared\_Layout.cshtml"
user can write CS script in frontend page using the next tags
@{
//C# code here
}
by default, razor page define a dictionary variable with name ViewData to exchange data between frontend and backend and used also, to send data to "Shared\_Layout.cshtml"
this ViewData concept replace ViewBag in older versions, that has performance issue.
the command will create DB context class and Entity(Tables) classes
Notes:
Entity(Tables) classes are just simple classes, representing the data, stored in the database.
The DB context class represents a session with the database and allows you to query and save instances of the entity classes.Sample Entity Framework Code
Razor Pages
each razor page consists of frontend and backend code
and any new razor project contains start page "index.cshtml" and it inherit page design from "Shared\_Layout.cshtml"
user can write CS script in frontend page using the next tags
@{
//C# code here
}
ViewData dictionary
by default, razor page define a dictionary variable with name ViewData to exchange data between frontend and backend and used also, to send data to "Shared\_Layout.cshtml"
this ViewData concept replace ViewBag in older versions, that has performance issue.
[ViewData] Attribute
Add [ViewData] attribute before any public variable allow access it through ViewData dictionary in frontend.
Note that any Model public variable can access also in frontend via the @Model.property
OnGet() , and OnPost()
Razor page by default handle two events (Get , and Post), but we can define more events using asp-page-handlerasp-page-handler="Delete" ==> OnPostDelete()
asp-page-handler="Edit" ==> OnPostEdit()
asp-page-handler="View" ==> OnPostView()
Redirect to another page
Redirect to another page with querystring parametersRazor Session variable
HttpContext.Session.SetString("Test String", "1");var x= HttpContext.Session.GetString("Test String");
No comments:
Post a Comment