Connecting ASP.NET Core MVC to SQL Server using EF Core.
This part will teach you how to:
Configure SQL Server connection
Create DbContext
Create database using EF Core migrations
Replace in-memory repository with real SQL database
Use async EF Core CRUD operations
Let’s begin.
Getting Started With ASP.NET Core MVC – Part 3
Connecting to SQL Server Using Entity Framework Core
1. Install EF Core Packages
Open Package Manager Console:
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Tools
2. Add Connection String
Open appsettings.json and add:
"ConnectionStrings": {
"DefaultConnection": "Server=YOUR_SERVER_NAME;Database=MVCAppDB;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Examples:
If using SQL Express:
Server=.\\SQLEXPRESS;Database=MVCAppDB;Trusted_Connection=True;
If using SQL Server authentication:
Server=localhost;Database=MVCAppDB;User Id=sa;Password=YourPassword123;
3. Create ApplicationDbContext
Create folder Data → file ApplicationDbContext.cs:
using Microsoft.EntityFrameworkCore;
using MVCApp.Models;
namespace MVCApp.Data
{
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Product> Products { get; set; }
}
}
✔ This DbContext represents your database.
4. Register EF Core in Program.cs
Open Program.cs:
using MVCApp.Data;
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
// Register DbContext
builder.Services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"));
});
var app = builder.Build();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Product}/{action=Index}/{id?}");
app.Run();
5. Enable EF Core Migrations
Open Package Manager Console:
Add-Migration InitialCreate
Output should say "Build succeeded."
Then:
Update-Database
✔ This creates the database and the Products table.
6. Remove Old In-Memory Repository
Delete/ignore your previous custom repository (ProductRepository).
Now your actual database will store products.
7. Update ProductController to Use EF Core
Open ProductController.cs:
using Microsoft.AspNetCore.Mvc;
using MVCApp.Data;
using MVCApp.Models;
using Microsoft.EntityFrameworkCore;
namespace MVCApp.Controllers
{
public class ProductController : Controller
{
private readonly ApplicationDbContext _db;
public ProductController(ApplicationDbContext db)
{
_db = db;
}
// GET: /Product
public async Task<IActionResult> Index()
{
var products = await _db.Products.ToListAsync();
return View(products);
}
// GET: Create
public IActionResult Create()
{
return View();
}
// POST: Create
[HttpPost]
public async Task<IActionResult> Create(Product product)
{
if (!ModelState.IsValid)
return View(product);
_db.Products.Add(product);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
// GET: Edit
public async Task<IActionResult> Edit(int id)
{
var product = await _db.Products.FindAsync(id);
if (product == null)
return NotFound();
return View(product);
}
// POST: Edit
[HttpPost]
public async Task<IActionResult> Edit(Product product)
{
if (!ModelState.IsValid)
return View(product);
_db.Products.Update(product);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
// GET: Delete
public async Task<IActionResult> Delete(int id)
{
var product = await _db.Products.FindAsync(id);
if (product == null)
return NotFound();
return View(product);
}
// POST: Delete
[HttpPost, ActionName("Delete")]
public async Task<IActionResult> DeleteConfirmed(int id)
{
var product = await _db.Products.FindAsync(id);
_db.Products.Remove(product);
await _db.SaveChangesAsync();
return RedirectToAction("Index");
}
}
}
✔ Repository removed
✔ Fully database-driven CRUD
✔ Async for better scalability
8. Run the Application
Now:
Click Add Product
Enter name + price
Save
Check the database:
SELECT * FROM Products
Your data is now stored in SQL Server 🎉
9. Part 3 Summary
You completed:
| Task | Status |
|---|---|
| Add EF Core packages | ✔ |
| Configure SQL Server | ✔ |
| Create DbContext | ✔ |
| Add migrations | ✔ |
| Connect MVC to real DB | ✔ |
| Replace repository | ✔ |
| Async CRUD | ✔ |
Your application is now a real production-ready MVC + SQL Server app.

Join the conversation! Your thoughts help the community grow.