![How to Connect ASP.NET Core with SQL Server A Beginner-Friendly CRUD Guide]()
Introduction
Most real-world applications need a database to store and manage data. In the .NET ecosystem, SQL Server is one of the most commonly used databases for building scalable applications.
ASP.NET Core works very well with SQL Server using Entity Framework Core, which simplifies database operations like creating, reading, updating, and deleting data.
In this blog, we will learn how to connect an ASP.NET Core application with SQL Server and perform basic CRUD operations.
What is CRUD?
CRUD represents the four basic operations used in databases:
These operations form the foundation of most backend applications.
Prerequisites
Before starting, make sure you have:
.NET SDK installed
SQL Server installed
Visual Studio or VS Code
Basic knowledge of C#
Step 1: Create a New ASP.NET Core Project
Open the terminal and run:
dotnet new webapi -n StudentAPI
Navigate into the project folder:
cd StudentAPI
Step 2: Install Entity Framework Core
Run the following command to install EF Core for SQL Server:
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
Step 3: Create a Model
Create a new folder called Models and add a class called Student.cs
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
This model represents a table in the database.
Step 4: Create a Database Context
Create a new folder Data and add AppDbContext.cs
using Microsoft.EntityFrameworkCore;
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
}
The DbContext manages database operations.
Step 5: Configure Database Connection
In appsettings.json, add your connection string:
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=StudentDB;Trusted_Connection=True;"
}
Step 6: Register the DbContext
In Program.cs, register the database context:
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
Step 7: Create a Controller
Create a controller called StudentsController
Example:
[ApiController]
[Route("api/[controller]")]
public class StudentsController : ControllerBase
{
private readonly AppDbContext _context;
public StudentsController(AppDbContext context)
{
_context = context;
}
[HttpGet]
public IActionResult GetStudents()
{
return Ok(_context.Students.ToList());
}
}
This endpoint retrieves all students from the database.
Testing the API
Run the application and open Swagger:
https://localhost:5001/swagger
You can test your API endpoints directly from the Swagger interface.
Why This is Important
Understanding how to connect ASP.NET Core with SQL Server helps developers build:
Web applications
REST APIs
Enterprise software
Data-driven systems
It is a fundamental skill for .NET backend developers.
Conclusion
Connecting ASP.NET Core with SQL Server using Entity Framework Core makes database operations simple and efficient. By learning CRUD operations, beginners can start building real-world applications and expand their backend development skills.