Prerequisites
Connection String Configuration
The database connection is configured inappsettings.json:
appsettings.json
The connection string uses SQL Server LocalDB with integrated security, which authenticates using your Windows credentials. No separate username or password is required.
Connection String Parameters
| Parameter | Value | Purpose |
|---|---|---|
| Data Source | (localdb)\\MSSQLLocalDB | LocalDB instance name |
| Initial Catalog | SupermarketEF | Database name |
| Integrated Security | True | Use Windows authentication |
| Encrypt | False | Disable connection encryption for local development |
Entity Framework Core Configuration
DbContext Registration
TheSupermarketContext is registered in Program.cs:
Program.cs
- Registers
SupermarketContextin the dependency injection container - Configures the SQL Server provider
- Reads the connection string from
appsettings.json
DbContext Implementation
TheSupermarketContext class in Data/SupermarketContext.cs defines all database tables:
Data/SupermarketContext.cs
DbSet<T> property represents a table in the database:
- Products - Product inventory items
- Categories - Product categories
- Customers - Customer information
- PayModes - Payment methods
- Users - Application users for authentication
Database Initialization
Create Initial Migration
Generate the first migration to create database schema:This creates a migration file in the
Migrations folder with the database schema definition.Apply Migration to Database
Create the database and apply the migration:This command:
- Creates the
SupermarketEFdatabase if it doesn’t exist - Applies all pending migrations
- Creates tables for all DbSet properties
Working with Migrations
Create a New Migration
After modifying model classes, create a new migration:Apply Migrations
Update the database with pending migrations:Rollback Migration
Revert to a specific migration:Remove Last Migration
Remove the most recent unapplied migration:Database Schema
The SupermarketWEB database includes the following entities:Product Table
Product Table
Id(int, primary key)Name(string)Price(decimal(6,2))Stock(int)CategoryId(int, foreign key)
Category Table
Category Table
Id(int, primary key)Name(string)Description(string, nullable)
Customer Table
Customer Table
Id(int, primary key)- Customer-specific fields (see Models/Customer.cs)
PayMode Table
PayMode Table
Id(int, primary key)- Payment mode fields (see Models/PayMode.cs)
User Table
User Table
Id(int, primary key)- Authentication fields (see Models/User.cs)
Troubleshooting
LocalDB Instance Not Found
If you encounter connection errors, verify LocalDB is installed:Migration Errors
If migrations fail, check:- Ensure the database is not in use by other applications
- Verify connection string credentials
- Check migration files for conflicts
- Review the error message for specific model or constraint issues
Connection Timeout
Increase the connection timeout inappsettings.json if needed:
Next Steps
- Learn about the Project Structure to understand how the application is organized
- Explore Razor Pages to see how data is accessed and displayed