ASP Dot Net With EntityFrameworkCore
Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).
Step 1: Create a New Replit Project
Go to https://replit.com
Sign in or create an account.
Click "Create"
In the "From Template" section, choose C# (.NET)
Give your project a name (e.g.,
dotnet-mvc-sqlite)Click "Create Repl"
Delete:
main.cs
main.csproj
Step 2: Configure .replit File
Replace the contents of .replit with the following:
run = "dotnet watch run --urls=http://0.0.0.0:8080"
hidden = ["bin", "obj"]
modules = ["dotnet-7.0"]
[env]
DOTNET_NOLOGO = "1"
DOTNET_CLI_TELEMETRY_OPTOUT = "1"
DOTNET_CLI_HOME = "$XDG_CACHE_HOME"
[gitHubImport]
requiredFiles = [".replit", "replit.nix"]
[nix]
channel = "stable-24_05"
[[ports]]
localPort = 8080
externalPort = 80
Step 3: Initialize an ASP.NET Core MVC Project at Root
In the Replit shell, run:
dotnet new mvc -o .
Step 4: Restore & Run the App
Run:
dotnet restore
dotnet watch run --urls=http://0.0.0.0:8080
Click the "Open in new tab" button or navigate to the provided URL.
You should see the default ASP.NET MVC home page.
Step 5: Add SQLite and EF Core Packages
Install required packages:
dotnet add package Microsoft.EntityFrameworkCore.Sqlite --version 7.0.20
dotnet add package Microsoft.EntityFrameworkCore.Design --version 7.0.20
Step 6: Install dotnet-ef CLI Tool Locally
To avoid runtime issues:
dotnet new tool-manifest
dotnet tool install dotnet-ef --version 7.0.20
Step 7: Create AppDbContext
1. Create Required Folders
In your Replit shell, create Data and Models directories if they don’t exist:
mkdir Data Models
2. Create Model Class (BlogPost.cs)
Create file: Models/BlogPost.cs
namespace MyMvcApp.Models
{
public class BlogPost
{
public int Id { get; set; }
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public DateTime CreatedAt { get; set; } = DateTime.Now;
}
}
3. Create AppDbContext.cs
Create file: Data/AppDbContext.cs
using Microsoft.EntityFrameworkCore;
using MyMvcApp.Models;
namespace MyMvcApp.Data
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<BlogPost> BlogPosts { get; set; }
}
}
Step 8: Register AppDbContext in Program.cs
Open Program.cs, add these using statements at the top:
using Microsoft.EntityFrameworkCore;
using MyMvcApp.Data;
Then, replace this line:
builder.Services.AddControllersWithViews();
with :
var connectionString = "Data Source=blog.db";
builder.Services.AddSqlite<AppDbContext>(connectionString);
builder.Services.AddControllersWithViews();
Step 9: Add and Apply Migration
Now that EF Core knows about your AppDbContext, you can create and apply a migration:
dotnet ef migrations add InitialCreate --project workspace.csproj
dotnet ef database update --project workspace.csproj
Note:
To undo this action, use 'ef migrations remove'
Resulting Structure
After completing all steps, your project should look like this:
/
├── Controllers/
├── Data/
│ └── AppDbContext.cs
├── Models/
│ └── BlogPost.cs
├── Views/
├── Program.cs
├── workspace.csproj
├── blog.db
└── Migrations/
Step: How to Check and Inspect the SQLite Database on Replit
Step 1: Confirm blog.db Exists
Run this command in the Replit shell:
ls -l blog.db
Step 2: Open the SQLite File Using CLI
Use the sqlite3 tool to open and inspect the database:
sqlite3 blog.db
Step 3: Run SQLite Commands
Inside the SQLite shell, try these commands:
.tables
.schema BlogPosts
SELECT * FROM BlogPosts;
Exit SQLite Shell
To exit the SQLite shell:
.quit