What is CRUD operation PART 3 With Example Code

Read operation involves retrieving existing records or entities from a database.
It typically includes querying the database to fetch specific data based on certain criteria, such as retrieving all records from a table, fetching a single record by its unique identifier, or filtering records based on certain conditions.
For example, in a web application, fetching a user's profile information or displaying a list of products would be considered read operations





using FreshWebApp.Models;
using Microsoft.EntityFrameworkCore;
 
namespace FreshWebApp.Data
{
    public class FreshWebAppDBContext : DbContext
    {
        public FreshWebAppDBContext(DbContextOptions<FreshWebAppDBContext> options) : base(options)
        {
        }
        public DbSet<ProductModel> ProductDetails  { get; set; }
        public DbSet<CategoryModel> ProductCategory  { get; set; }
        public DbSet<OrderModel> Order { get; set; }
        public DbSet<OrderItemModel> OrderItem  { get; set; }
    }
}




{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DataConnectionString": "Data Source=LAPTOP;Initial Catalog=Testing;Integrated Security=True;Trust Server Certificate=True;"
  }
}


using FreshWebApp.Data;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;
 
var builder = WebApplication.CreateBuilder(args);
 
builder.Services.AddControllersWithViews();
builder.Services.AddDbContext<FreshWebAppDBContext>(option => option.UseSqlServer(builder.Configuration.GetConnectionString("DataConnectionString")));
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddMvc();
builder.Services.AddHttpContextAccessor();
 
 
builder.Services.Configure<KestrelServerOptions>(options =>
{
    options.Limits.MaxRequestBodySize = int.MaxValue;
});
builder.Services.AddDistributedMemoryCache();
builder.Services.AddSession(options =>
{
    options.IdleTimeout = TimeSpan.FromMinutes(1000);
});
 
var app = builder.Build();
 
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseHsts();
}
 
app.UseStaticFiles(new StaticFileOptions()
{
    FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot\Images")),
    RequestPath = new PathString("/wwwroot/Images")
});
 
app.UseSession();
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
 
app.MapAreaControllerRoute(
    name: "Admin",
    areaName: "Admin",
    pattern: "Admin/{controller=Product}/{action=Index}/{id?}");
 
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=AddToCart}/{id?}");
 
app.Run();