Aspnet Core Razor Pages integrated with datatables + CRUD + SQLite
dotnet new webapp -o websiteName
code -r websiteName
👉 Remember to change websiteName
at all places where it is uses to avoid errors
dotnet dev-certs https --trust
- Add a folder named Models
- Add a class to the Models folder named ModelName(As per your choice) Here I have kept it Product
- Class Example:
using System.ComponentModel.DataAnnotations;
namespace websiteName.Models;
public class ModelName
{
public int Id { get; set; } // For ID Data
public string? Title { get; set; } // For Title Data
public decimal Price { get; set; } // For Price Data
[DataType(DataType.Date)]
public DateTime ExpiryDate { get; set; } { get; set; } // For Expiry Date Data
}
dotnet tool uninstall --global dotnet-aspnet-codegenerator
dotnet tool install --global dotnet-aspnet-codegenerator // Scaffolding Tool
dotnet tool uninstall --global dotnet-ef
dotnet tool install --global dotnet-ef // Tools for EF Core
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SQLite // EF SQLite Provider
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer // Package for Scaffolding
dotnet add package Microsoft.EntityFrameworkCore.Tools // Package for Scaffolding
dotnet aspnet-codegenerator razorpage -m ModelName(Product) -dc websiteName.Data.websiteNameContext -udl -outDir Pages/Products --referenceScriptLibraries --databaseProvider sqlite
``` dotnet aspnet-codegenerator razorpage -h // to get help on generator command ```
dotnet ef migrations add InitialCreate
dotnet ef database update
dotnet build // build the app
dotnet watch // run the app with auto reload
dotnet run // run the app without auto reload
- Change the
<title>
element in thePages/Shared/_Layout.cshtml
file to displayModelName(Product)
rather thanwebsiteName
.
<title>@ViewData["Title"] - ModelName</title>
- Find the following anchor element in the
Pages/Shared/_Layout.cshtml
file
<a class="navbar-brand" asp-area="" asp-page="Products/Index">Products</a>
-
download SQLite or
-
Run in terminal
brew install sqlite
& check its versionsqlite3 --version
💻 -
download DB Browser for SQLite or
- Create a new class named SeedData in the Models folder with the following code:
using Microsoft.EntityFrameworkCore;
using websiteName.Data;
using System;
namespace websiteName.Models
{
public static class SeedData
{
public static void Initialize(IServiceProvider serviceProvider)
{
using (var context = new websiteNameContext(
serviceProvider.GetRequiredService<DbContextOptions<websiteNameContext>>()))
{
if (context == null || context.Product == null)
{
throw new ArgumentNullException("Null websiteNameContext");
}
// Look for any products.
if (context.Product.Any())
{
return; // DB has been seeded
}
context.Product.AddRange(
new Product
{
Name = "Product 1",
Price = 10.99M,
ExpiryDate = DateTime.Parse("2023-10-01")
},
new Product
{
Name = "Product 2",
Price = 19.99M,
ExpiryDate = DateTime.Parse("2023-09-15")
},
new Product
{
Name = "Product 3",
Price = 5.49M,
ExpiryDate = DateTime.Parse("2023-11-05")
},
new Product
{
Name = "Product 4",
Price = 14.99M,
ExpiryDate = DateTime.Parse("2023-10-20")
},
new Product
{
Name = "Product 5",
Price = 67.99M,
ExpiryDate = DateTime.Parse("2023-10-20")
}
);
context.SaveChanges();
}
}
}
}
Open the database accessing it from db browser (locate generated .db file) & update database with CLI
- Use
dotnet ef migrations add updateName
&dotnet ef database update
to update data to local database.