Database Concepts  «Prev  Next»

Configure a SQL Server Connection - Exercise

Connect the VirtualBookShelf application to SQL Server 2025

Objective: Configure an ASP.NET Core Razor Pages application to access an existing SQL Server 2025 database through Entity Framework Core.

Background

The modern equivalent of the legacy Data Environment is named configuration plus a database context registered with dependency injection. You do not add a Visual InterDev Data Environment, edit Global.asa, or attach the old virtualbook.mdb Access file.

Use BigBook as the sample project and database name and BigBookConn as the configuration key. This is the VirtualBookShelf course application under the name shown in the illustration. The preceding lesson uses VirtualBookShelfConnection; names are configurable, but the configuration key and code must agree throughout your project.

Prerequisites

  • A C# ASP.NET Core Web App (Razor Pages) project named BigBook. Retain the template's middleware and security configuration.
  • For the Visual Studio 2022 course setup, use an IDE-compatible target such as .NET 8 with EF Core 8, or .NET 9 with EF Core 9, with current servicing patches. .NET 10 requires Visual Studio 2026 (18.0+) for supported IDE targeting. Check the linked support policy before starting a new project.
  • Install Microsoft.EntityFrameworkCore.SqlServer through Manage NuGet Packages using the matching EF Core major version. This provider uses Microsoft.Data.SqlClient.
  • An instructor-provisioned BigBook database on SQL Server 2025, its actual host and port, and permission for your Windows account to connect. No sample tables or Access download are needed for this connection-only exercise.
  • A trusted server certificate whose name matches the connection host. Ask the administrator for the correct endpoint and trust configuration if necessary.

Instructions

  1. Add the following ConnectionStrings entry to appsettings.json, merging it with the existing JSON. Replace the example host and port with your assigned SQL Server endpoint. Do not paste credentials into your submission.
{
  "ConnectionStrings": {
    "BigBookConn": "Server=tcp:sql2025-host,1433;Database=BigBook;Integrated Security=True;Encrypt=True;TrustServerCertificate=False;"
  }
}

Integrated Security=True uses the process's Windows identity. When launched locally from Visual Studio this is normally your Windows account; a hosted application may use a different identity. This sample intentionally contains no password. Use development User Secrets or an approved production secret store if your assigned authentication method requires secrets.

  1. Create Data/BigBookContext.cs with a BigBookContext : DbContext class. Its public constructor must accept DbContextOptions<BigBookContext> and pass those options to the base constructor. No entity sets are needed for this connection check.
  2. In Program.cs, register the context before builder.Build(). Use AddDbContext<BigBookContext>, UseSqlServer, and GetConnectionString("BigBookConn"). Keep the template's Razor Pages registration and middleware.
  3. Build the project, then add this temporary development-only check after builder.Build() and before app.Run(). Run in the Development environment and inspect the application log in Visual Studio's Output window or the console.
// Temporary connection check, after builder.Build() and before app.Run().
if (app.Environment.IsDevelopment())
{
    await using var scope = app.Services.CreateAsyncScope();
    var db = scope.ServiceProvider.GetRequiredService<BigBookContext>();
    bool connected = await db.Database.CanConnectAsync();
    app.Logger.LogInformation("BigBook connection check: {Connected}", connected);
}

A True result establishes connectivity, not table availability, query permissions, or schema correctness. A False result requires investigation of the host, network, database name, Windows login, or certificate trust. Do not disable certificate validation to hide a connection problem. Remove the temporary check after recording your result.

What to submit

Submit your redacted connection configuration, context constructor, registration code, and observed connection-check result. If you cannot run the application, say so and explain the expected check; do not claim an unobserved success. Do not include passwords, tokens, or private infrastructure details.

Self-assessment: 3 points

Submitting displays your response and a model solution. This page does not run your C# code, connect to your database, or automatically award points. Compare your work with this rubric.

  1. 1 point: The named connection targets the intended database and uses appropriate authentication, encryption, and certificate validation.
  2. 1 point: BigBookContext accepts typed options and is registered with AddDbContext and UseSqlServer.
  3. 1 point: You report a successful connection check or accurately document a failure, its likely cause, and the next diagnostic step. Explain why a project tree alone does not prove connectivity.

Microsoft documentation