liveBook · Manning

Connection string

As explained in Building Web APIs with ASP.NET Core

A connection string is a configuration value that specifies how an application connects to a data source, often including server, database, and credentials.

A connection string tells the board-game API which SQL Server database to use and how to connect. The example names a server and database and includes login credentials. EF Core uses the configured value to connect ApplicationDbContext to the database. section 4.3.4 section 4.5

Set up the connection in this order:

  • Create the database, then create a SQL Server login and a database user linked to it. The login authenticates to the instance; the user controls access to that database. section 4.2.4
  • Store the development connection string outside source control. Visual Studio Secret Manager is one option; an environment variable or an ignored development settings file are alternatives. section 4.3.4
  • Register ApplicationDbContext with the SQL Server provider and retrieve the value named DefaultConnection. Once connected, EF Core migrations can create the database structure from the data model. section 4.3.4 section 4.5
builder.Services.AddDbContext<ApplicationDbContext>(options =>
   options.UseSqlServer(
       builder.Configuration.GetConnectionString("DefaultConnection"))
   );

This Program.cs registration passes DefaultConnection from configuration to the SQL Server provider. section 4.3.4

{
 "ConnectionStrings": {
   "DefaultConnection": "Server=localhost\\SQLEXPRESS;Database=MyBGList;
       User Id=MyBGList;Password=MyS3cretP4$$;
       Integrated Security=False;MultipleActiveResultSets=True;
       TrustServerCertificate=True"
 }
}

This development example shows the ConnectionStrings configuration key and a SQL Server value containing server, database, user ID, and password. section 4.3.4

Production needs its own database login, user, and connection string. Give the production login a different password from the development login. section 12.3.6

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Shows credential setup, configuration storage, and DbContext registration, but does not dissect the connection-string options.
Examples
Uses a MyBGList SQL Server value named DefaultConnection and a Program.cs AddDbContext registration.
What it assumes
Assumes an ASP.NET Core API with ApplicationDbContext; walks through creating the database login and user needed for its credentials.
What you can do after
Apply it via EF Core to configure ApplicationDbContext for SQL Server and then create database structure with migrations.
How it connects
Links SQL Server logins and users, ASP.NET Core configuration, EF Core DbContext registration, and migrations.

Also explained in

Compare how 7 books explain connection string →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.