Connecting to a database is one of the most essential steps in building any web application. Whether you are creating a login system, storing user data, managing products, or generating reports, your ASP.NET Core application must communicate with a database.
Microsoft SQL Server is one of the most widely used relational databases in enterprise applications. ASP.NET Core provides seamless integration with SQL Server using Entity Framework Core (EF Core), making database operations simple and efficient.
In this article, we will learn how to connect SQL Server to an ASP.NET Core application step by step.
Why Database Connection Is Important
Database connectivity is required for:
User authentication systems
CRUD (Create, Read, Update, Delete) operations
E-commerce applications
Admin dashboards
Reporting systems
Enterprise applications
Without a database connection, applications cannot store or retrieve persistent data.
Step 1: Install Required Packages
To connect SQL Server with ASP.NET Core, install the following NuGet packages:
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.Tools
These packages allow EF Core to communicate with SQL Server.
Step 2: Add Connection String in appsettings.json
The connection string contains the database server details, database name, and authentication information.
{
"ConnectionStrings": {
"DefaultConnection": "Server=YOUR_SERVER_NAME;Database=YourDatabaseName;Trusted_Connection=True;TrustServerCertificate=True;"
}
}Explanation
Server → SQL Server instance name
Database → Name of your database
Trusted_Connection=True → Uses Windows Authentication
TrustServerCertificate=True → Avoids SSL certificate issues (for development)
In production, you should secure credentials properly.
Step 3: Create DbContext and Model (Main Database Configuration)
Now create a model class and a DbContext class.
Example Model and DbContext
using Microsoft.EntityFrameworkCore;
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
public DbSet<Student> Students { get; set; }
}What Is Happening Here?
Student is a model class representing a database table.
Sanjay JoshiPosted Feb 27, 2026, 8:29 PM
Nice article