Tutorials ASP.NET Core MVC Tutorial

Connection Strings — Complete Guide

Connection Strings — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of ASP.NET Core MVC Tutorial on Toolliyo Academy.

On this page

ASP.NET Core MVC Tutorial · Lesson 136 of 200

Connection Strings

Getting Started ✓Core MVC ✓Data & Security ✓Production ✓Career ✓

Interview Ready · 10 — Interview Prep · ~10 min · Section 15: Database & EF Core

What is this?

EF Core setup means adding NuGet packages, defining a DbContext class, putting a connection string in appsettings.json, and registering the context in Program.cs.

Why should you care?

ShopNest needs SQL Server to save products and orders. EF Core is how C# objects become database tables.

See it live — copy this example

Create an MVC project (dotnet new mvc), add the code, and run dotnet run.

// appsettings.json
"ConnectionStrings": {
  "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=ShopNest;Trusted_Connection=True;"
}

// Program.cs
builder.Services.AddDbContext<ShopNestDbContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

Run Example »

Edit the code and click Run — like W3Schools Try it Yourself.

Code
Result

What happened?

  • DbContext is the gateway to the database.
  • AddDbContext registers it per HTTP request (scoped).
  • Connection string points at SQL Server or LocalDB.

Try it yourself

  1. dotnet add package Microsoft.EntityFrameworkCore.SqlServer
  2. Create ShopNestDbContext : DbContext with DbSet.
  3. Add connection string and register in Program.cs.
  4. Change text or labels in the example and run again — watch the browser update.
  5. Break the code on purpose (remove a semicolon), read the error message, then fix it.

Remember

EF Core + SQL Server + DbContext + connection string. Register in Program.cs. Migrations create tables.

Interview prep for this lesson

Practice these questions aloud after reading—each links to a full structured answer.

Mid PDF Detailed
Access in code: builder.Configuration["ConnectionStrings:Default"]; ✅ Stored securely under your user profile (not in the project folder). 🌍 5. How do you use environment variables?
Short answer: Environment variables are great for overriding settings at deployment time (especially in Docker or Azure). Example: set ASPNETCORE_ENVIRONMENT=Production set ConnectionStrings__Default=&quot;Server=mydb;Da…
Mid PDF Detailed
SignalR vs WebSockets Feature WebSockets SignalR Protocol Low-level TCP-based High-level abstraction Transpor t Only WebSockets Fallbacks: WebSockets → SSE → Long Polling Usage Real-time messages Real-time hubs with automatic reconnection, grouping, scaling ✅ Use SignalR for apps needing broadcast, connection management, and fallbacks.
Short answer: SignalR vs WebSockets Feature WebSockets SignalR Protocol Low-level TCP-based High-level abstraction Transpor Only WebSockets Fallbacks: WebSockets → SSE → Long Polling Usage Real-time messages Real-time hu…
Mid PDF Detailed
Using SignalR for Real-Time Communication // Hub public class ChatHub : Hub { public async Task SendMessage(string user, string message) => await Clients.All.SendAsync("ReceiveMessage", user, message); } // Client (JavaScript) const connection = new signalR.HubConnectionBuilder() .withUrl("/chathub") .build(); connection.on("ReceiveMessage", (user, message) => { console.log(`${user}: ${message}`); }); await connection.start(); await connection.invoke("SendMessage", "Alice", "Hello!");
Short answer: Using SignalR for Real-Time Communication // Hub public class ChatHub : Hub { public async Task SendMessage(string user, string message) =&gt; await Clients.All.SendAsync(&quot;ReceiveMessage&quot;, user, m…
Mid PDF Detailed
Health Checking in ASP.NET Core builder.Services.AddHealthChecks() .AddSqlServer(connectionString) .AddCheck<CustomHealthCheck>("CustomCheck"); app.MapHealthChecks("/health"); Custom Health Check Example: public class CustomHealthCheck : IHealthCheck { public Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken token) => Task.FromResult(HealthCheckResult.Healthy("Everything OK")); }
Short answer: Health Checking in ASP.NET Core builder.Services.AddHealthChecks() .AddSqlServer(connectionString) .AddCheck&lt;CustomHealthCheck&gt;(&quot;CustomCheck&quot;); app.MapHealthChecks(&quot;/health&quot;); Cust…
Mid PDF Detailed
Rate Limiting Middleware in .NET 8 builder.Services.AddRateLimiter(options => { options.GlobalLimiter = PartitionedRateLimiter.Create<HttpContext, string>(httpContext => RateLimitPartition.GetFixedWindowLimiter(httpContext.Connection.Remo teIpAddress?
Short answer: Rate Limiting Middleware in .NET 8 builder.Services.AddRateLimiter(options =&gt; { options.GlobalLimiter = PartitionedRateLimiter.Create&lt;HttpContext, string&gt;(httpContext =&gt; RateLimitPartition.GetFi…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

ASP.NET Core MVC Tutorial
Course syllabus

ASP.NET Core MVC Tutorial

Section 1: Introduction & Environment Setup
Section 2: ASP.NET Core Basics & Hosting
Section 3: Controllers
Section 4: Views & Razor
Section 5: Models & Data Passing
Section 6: Routing
Section 7: Dependency Injection
Section 8: Action Results
Section 9: HTML Helpers
Section 10: Tag Helpers
Section 11: Model Binding
Section 12: Validation
Section 13: State Management
Section 14: Filters
Section 15: Database & EF Core
Section 16: Authentication
Section 17: File Handling
Section 18: Advanced MVC
Section 19: Performance
Section 20: Deployment
Section 21: Enterprise Development
Section 22: Real-World Projects
Section 23: Interview Preparation
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details