using Microsoft.EntityFrameworkCore;namespace BookieWeb.Models
{publicclassApplicationDbContext:DbContext{publicApplicationDbContext(DbContextOptionsoptions):base(options){}// This is where we will be adding the }}
Changes made in program.cs
using Microsoft.EntityFrameworkCore;varbuilder= WebApplication.CreateBuilder(args);// Add services to the container.
builder.Services.AddControllersWithViews();// adding database connection
builder.Services.AddDbContext<BookieWeb.Models.ApplicationDbContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));varapp= builder.Build();// Configure the HTTP request pipeline.if(!app.Environment.IsDevelopment()){
app.UseExceptionHandler("/Home/Error");// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name:"default",
pattern:"{controller=Home}/{action=Index}/{id?}");
app.Run();
Database Migration
Run following commands
Add-Migration "migration message"
Update-database
Add data to tables using program
Addin following lines of code to add the data
using BookieWeb.Models;using Microsoft.EntityFrameworkCore;namespace BookieWeb.Models
{publicclassApplicationDbContext:DbContext{publicApplicationDbContext(DbContextOptionsoptions):base(options){}// This is where we will be adding the publicDbSet<Category> Categories {get;set;}protectedoverridevoidOnModelCreating(ModelBuildermodelBuilder){
modelBuilder.Entity<Category>().HasData(new Category {Id=1,Name="Action",DisplayOrders=1},new Category {Id=2,Name="History",DisplayOrders=2},new Category {Id=3,Name="History",DisplayOrders=2});}}}
In contropller informatino is given to View as follows
public IActionResult Index(){// Get list items from the databaseList<Category>objCategoryList= _db.Categories.ToList();// Default View if no view is providedreturn View(objCategoryList);}
First model is added to the start of cshtml as given below
@model List<Category>
Run followinh line to loop over the information that is being gievn to View as follows
<tableclass="table table-bordered table-striped"><thead><!-- head of the table --><tr><!--Adding header column for the table --><th> Category Name</th><th> Display Order</th></tr></thead><tbody><!-- Adding for loop to iterate over the list elements --> @foreach(varobjin Model.OrderBy(u=> u.DisplayOrders)){<!-- body of the table --><tr><td>
@obj.Name
</td><td>
@obj.DisplayOrders
</td></tr>}</tbody></table>
Adding partial views
Add following lines of code in View to add parial view
This Notification.csthml partial view will be added to the top of View
<!-- partial tag will be used here to include the notifications --><partialname="_Notification" />
This partial view will be added to Layout.cshtml*
<divclass="container"><mainrole="main" class="pb-3"><!-- partial tag will be used here to include the notifications --><partialname="_Notification" />
@RenderBody()
</main></div>